Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.

Similar presentations


Presentation on theme: "Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to."— Presentation transcript:

1 Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to a text file Closing open files

2 Unit 202 Files There are two types of files: –Binary files: should be handled as a sequence of bits. –Text files: should be handled as a sequence of characters. Files are used for two major reasons: 1.To store data permanently. 2.To handle large amounts of data easily. Java uses streams to handle files, like all other I/O types.

3 Unit 203 Opening a File for Reading To open a file for reading, the classes FileReader and BufferedReader (from java.io) are used BufferedReader file1 = new BufferedReader(new FileReader(filename)); –filename can be a constant or variable String. –It can have a file name or the file name with its full path. –Examples: “myfile.txt”, “C:\\homework\\StudentTest.java”, “C:/homework/StudentTest.java”

4 Unit 204 Reading from a Text File BufferedReader has two methods that can be used for reading: read() and readLine(). The same methods are used when reading from a file. The method read() reads a single character and returns character code as integer. A type cast should be used to convert it to a character. –For example: char c = (char) file1.read(); If read() attempts to read beyond the end of file, the value -1 is returned. The method readLine() reads a full line of the file and returns a String. –For example: String s = file1.readLine(); If readLine() attempts to read beyond the end of file, the value null is returned.

5 Unit 205 Example 1 –public static void main(String[] args){ – try{ – BufferedReader inputStream = new BufferedReader(new – FileReader("c:/ics102/grades.txt")); – int count = 0; double sum = 0; String line; – while((line = inputStream.readLine()) != null){ – double grade = Double.parseDouble(line); – sum += grade; – count ++; – } – – System.out.println("average is " + sum/count); – inputStream.close(); – }catch(FileNotFoundException e){ } – catch(IOException e){} – } File grades.txt: 3.0 4.0 5.0 6.0 Output: The average is 4.5

6 Unit 206 Opening a file for Writing/Appending To open a file for writing, the classes FileWriter and PrintWriter (from java.io) are used as follows: PrintWriter file2 = new PrintWriter(new FileWriter(filename)); –filename has the same rules as mentioned earlier. –This statement will create a new file if the file doesn’t exist. –If a file exists with the same name, this statement will erase it. To open a file for appending, the following statement is used instead: PrintWriter file2 = new PrinWriter(new FileWriter(filename, true)); –This statement will not erase an existing file. All data written will be appended to the end of the file. –If the file doesn’t exist, it will be created.

7 Unit 207 Writing to a Text File Two methods can be used to write to an opened text file, print() and println(). We have seen these methods before. The method print() will print without generating a new line. The method println() will print and generate a new line. These methods are used in the same way as the methods in System.out

8 Unit 208 Closing a Stream When a stream is no longer needed, it is better to close it. Both BufferedReader and PrintWriter have a method called close() that can be used to close each of these streams. –For example: file1.close(); Closing an input stream is optional. The system will close it when the program finishes. However, closing a PrintWriter is not optional. If the programmer doesn’t close it, all the changes made to the file will be lost. To keep your changes, close output files when you finish writing.

9 Unit 209 Example: Write to a file The following program copies everything in the file “source.txt” into the file “destination.txt”. import java.io.*; class FileCopying { public static void main(String args[]) throws IOException { BufferedReader inFile = new BufferedReader(new FileReader("source.txt")); PrintWriter outFile = new PrintWriter(new FileWriter("destination.txt")); String line = inFile.readLine(); while(line != null) { outFile.println(line); line = inFile.readLine(); } inFile.close(); outFile.close(); }


Download ppt "Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to."

Similar presentations


Ads by Google