0% found this document useful (0 votes)
39 views17 pages

OOP's (CS3391)

The document discusses reading and writing files in Java. It explains how to create a file, write to a file, and read from a file using Java classes like File, FileWriter, and Scanner. Example programs are provided for each task.

Uploaded by

stevecanses
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views17 pages

OOP's (CS3391)

The document discusses reading and writing files in Java. It explains how to create a file, write to a file, and read from a file using Java classes like File, FileWriter, and Scanner. Example programs are provided for each task.

Uploaded by

stevecanses
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

READING AND WRITING

OOP’s (CS3391) FILES


UNIT – 4 I/O, GENERICS, STRING
HANDLING
 File handling in Java implies reading
from and writing data to a file.
 The File class from the java.io package,
Reading and allows us to work with different formats
of files.
Writing Files  In order to use the File class, you need
to create an object of the class and
specify the filename or directory name.
The File class has many useful methods for creating and getting information about files.
For example:

Reading and
Writing Files
To create a file in Java, you
can use the createNewFile()
method. This method
returns a boolean value: true
1) Create a File if the file was successfully
created, and false if the file
already exists.
import java.io.File;
import java.io.IOException;
public class CreateFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " +
myObj.getName());

Eg program } else {
System.out.println("File already exists.");

to CREATE }
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
SS 1.1 JAVA program to create the file is COMPILED
SS 1.2 New File has been created
In the following example,
we use the FileWriter class
together with its write()
method to write some text to
the file we created in the
2) Write a File example above. Note that
when we are done writing to
the file, we should close it
with the close() method
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {


public static void main(String[] args) {
try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Our java professor is Darwin and we
thank him for the effort he takes to take us for an

Eg program IV");
myWriter.close();

toWRITE System.out.println("Successfully wrote to the file.");


} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
SS 2.1 Program to Write the written File
SS 2.2 The file has been Wrote successfully
In the following
example, we use the
Scanner class to read the
3) Read a File
contents of the text file
we created in the
previous example
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();

Eg program System.out.println(data);
}

to READ myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
SS 3.1 Program to Read the written File
SS 3.2 The file has been read successfully
Thankyou!
do you have any questions?

ADD NAME ADD NAME


Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet,
consectetur adipiscing elit. consectetur adipiscing elit.

ADD NAME ADD NAME


Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet,
consectetur adipiscing elit. consectetur adipiscing elit.

You might also like