Java Programming Istream
Java Programming Istream
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 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 n this section.
FILE HANDLING
Reading from a file To read from a file, you can use the FileInputStream class. Here is one of the constructors of this class. FileInputStream(String filename) The constructor creates a connection to an actual file whose filename s specified as an argument. A FileNotFoundException is thrown when the file does not exist or it cannot be opened
FILE HANDLING
After creating an input stream, you can now use 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.
import java.io.*; public class ReadFile { public static void main(String args[])throws IOException { System.out.println("What is the name of the file to read from?"); String filename; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); filename = br.readLine(); System.out.println("Now filename + "..."); reading from "
FILE HANDLING
try{ char data; int temp; do{ temp = fis.read(); data = (char) temp; if (temp != 1) System.out.print(data); }while (temp != -1);
in
reading
FileInputStream fis = null; try{ fis = new FileInputStream(filename); }catch (FileNotFoundException ex) {
} }
FILE HANDLING
Assuming test.txt exist 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? test.txt Now reading from temp.txt temporary file
FILE HANDLING
Writing to a file For writing to a file, you can use the FileOutputStream class. Here is one of the constructors you can use. FileOutputStream(String 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 write(int b)
try{ else{ package sd1a; fos = new fos.write('q'); FileOutputStream(filename); import java.io.*; fos.write(data); }catch(FileNotFoundException ex) } { public class WriteFile { }else{ System.out.println("Fiile cannot public static void main(String args[]) fos.write(data); be opened for writing."); throws IOException } } { }while(!done); try{ System.out.println("What is the name }catch(IOException ex) of the file to be written to?"); boolean done = false; { int data; System.out.println("Problem in String filename; reading from the file."); BufferedReader br = new do{ } BufferedReader(new data = br.read(); InputStreamReader(System.in)); if((char) data == 'q') { } filename = br.readLine(); data = br.read(); } System.out.println("Enter data to if((char)data == '$') write to " + filename +"..."); { System.out.println("Type q$ to end."); done = true; } FileOutputStream fos = null;
FILE HANDLING