Presentation is loading. Please wait.

Presentation is loading. Please wait.

File IO Basics By Dan Fleck Coming up: Data Streams.

Similar presentations


Presentation on theme: "File IO Basics By Dan Fleck Coming up: Data Streams."— Presentation transcript:

1 File IO Basics By Dan Fleck Coming up: Data Streams

2 Data Streams In Java data is read to a file using an input stream. Data is written to a file using an output stream Streams are a sequence of data. Input streams can come from files, the console (System.in for example), a network connection, etc… Output streams can send data to files, the console (System.out), data pipes, etc… Coming up: Data Streams

3 Data Streams The good news is from your program’s perspective all incoming text data is the same because it uses the java.io.Reader All outgoing text data uses java.io.Writer Intuition: Think from the viewpoint of your program (input is coming INTO your program, output is data leaving your program) Coming up: Reading a file one char at a time

4 Reading a file one char at a time public static void main(String[] args) { FileReader inputStream = null; // Declare the variable try { // Construct the stream inputStream = new FileReader(”myText.txt"); int c = inputStream.read(); while (c != -1) { // Did we get to the end of the stream? System.out.println(“Char: “+c); c = inputStream.read(); // read the next char } } catch (IOException ioe) { // comes here if any errors occur ioe.printStackTrace(); System.out.println(“There was an error reading the file!”); } finally {// always close the file if (inputStream != null) inputStream.close(): } Don’t do this! Coming up: Buffering

5 Buffering For performance reasons, you usually want to read files in bigger chunks than one character. Thus, use a BufferedReader instead! Constructing a BufferedReader using a FileReader as the input. This creates a chain of streams output from FileReader is input to BufferedReader inputStream = new BufferedReader(new FileReader(”file.txt")); Coming up: Reading lines of text

6 Reading lines of text [Add code from prev example here ] BufferedReader inputStream = null; // Declare the variable try { // Construct the stream inputStream = new BufferedReader(new FileReader(”myText.txt")); // Read a line of text from the file String line = inputStream.readLine(); while (line != null) { // Did we get to the end of the stream? System.out.println(“Line: “+line); line = inputStream.readLine(); // read the next line of text } [Add code from prev example here ] Read files this way! Coming up: Writing Character Files

7 Writing Character Files Writing is similar to reading, except we use a FileWriter FileWriter output = new FileWriter(“someFile.txt”); output.write(‘a’); // Write a char output.write(“A longer string”); // Write a string output.close(); // Always close the stream!!!! BUT… don’t forget to buffer for performance! Coming up: Writing Files

8 Writing Files BufferedWriter bw = new BufferedWriter(new FileWriter(“myFile.txt”)); But, what if you want to use println and like System.out does? Create a PrintWriter from your BufferedWriter! PrintWriter output = new PrintWriter(bw); // Make a PrintWriter output.println(123); output.println(“This is some text”); System.out is a PrintWriter attached to the console… you can make your own PrintWriter attaching it to Files, etc... Coming up: Character versus Binary Files

9 Character versus Binary Files FileReader and FileWriter are for use in reading/writing character-based (text) files. To read/write binary files like images or sounds or any non-text file, use byte-stream classes TextBinary WriterOutputStream ReaderInputStream BufferedReaderBufferedInputStream BufferedWriterBufferedOutputStream FileWriterFileOutputStream FileReaderFileInputStream Coming up: Summary

10 Summary Reading –Create an input stream from your data source –Read the stream until all data is gone –Close the stream Writing –Create an output stream to your data source –Write to the stream –Close the stream Coming up: References

11 References Reference: http://java.sun.com/docs/books/tutorial/e ssential/io/index.htmlhttp://java.sun.com/docs/books/tutorial/e ssential/io/index.html Find out more by going to the link! End of presentation


Download ppt "File IO Basics By Dan Fleck Coming up: Data Streams."

Similar presentations


Ads by Google