Reading from a File Writing to a File

J.E.D.I. exception in the throws clause of the method.

5.4 File Handling

In some cases, data inputs are stored in files. Moreover, there are also instances when we want to store the output of a certain program to a file. In a computerized enrollment system, the student data that may be used as an input to the system is most probably stored in a particular file. Then, one possible output of the system is the information about the subjects enrolled in by the students. Again, the output in this case may preferably be stored in a file. As seen in this application, there is a need for reading from a file and writing to a file. You will learn about file input and output in this section.

5.4.1 Reading from a File

To read from a file, you can use the FileInputStream class. Here is one of the constructors of this class. FileInputStreamString filename The constructor creates a connection to an actual file whose filename is specified as an argument. A FileNotFoundException is thrown when the file does not exist or it cannot be opened for reading. After creating an input stream, you can now use the stream to read from the associated file using the read method. The read method returns an integer and it returns -1 when the end of the file is reached. Here is an example. import java.io.; class ReadFile { public static void mainString args[] throws IOException { System.out.printlnWhat is the name of the file to read from?; String filename; BufferedReader br = new BufferedReadernew InputStreamReaderSystem.in; filename = br.readLine; System.out.printlnNow reading from + filename + ...; FileInputStream fis = null; try { fis = new FileInputStreamfilename; } catch FileNotFoundException ex { System.out.printlnFile not found.; } try { char data; int temp; do { temp = fis.read; data = char temp; if temp = -1 { Introduction to Programming II Page 76 J.E.D.I. System.out.printdata; } } while temp = -1; } catch IOException ex { System.out.printlnProblem in reading from the file.; } } } Assuming temp.txt exists and it contains the text temporary file, here is a sample output of this program: What is the name of the file to read from? temp.txt Now reading from temp.txt... temporary file

5.4.2 Writing to a File

For writing to a file, you can use the FileOutputStream class. Here is one of the constructors you can use. FileOutputStreamString filename The constructor links an output stream to an actual file to write to. A FileNotFoundException is thrown when the file cannot be opened for writing. Once the output stream is created, you can now use the stream to write to the linked file using the write method. The method has the following signature: void writeint b The parameter b refers to the data to be written to the actual file associated to the output stream. The following program demonstrates writing to a file. import java.io.; class WriteFile { public static void mainString args[] throws IOException { System.out.printlnWhat is the name of the file to be written to?; String filename; BufferedReader br = new BufferedReadernew InputStreamReaderSystem.in; filename = br.readLine; System.out.printlnEnter data to write to + filename + ...; System.out.printlnType q to end.; FileOutputStream fos = null; try { fos = new FileOutputStreamfilename; } catch FileNotFoundException ex { System.out.printlnFile cannot be opened for writing.; } Introduction to Programming II Page 77 J.E.D.I. try { boolean done = false; int data; do { data = br.read; if chardata == q { data = br.read; if chardata == { done = true; } else { fos.writeq; fos.writedata; } } else { fos.writedata; } } while done; } catch IOException ex { System.out.printlnProblem in reading from the file.; } } } Here is a sample run of WriteFile: What is the name of the file to be written to? temp.txt Enter data to write to temp.txt... Type q to end. what a wonderful world 1, 2, step q When you open temp.txt, it should contain the following: what a wonderful world 1, 2, step Introduction to Programming II Page 78 J.E.D.I.

5.5 Exercises