Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.

Similar presentations


Presentation on theme: "Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming."— Presentation transcript:

1 Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming

2 SE15: Streams and File I/O30–2 Today’s Learning Objectives To become familiar with the concept of an I/O stream. Understand the difference between binary and text files. Learn how to save data in a file. Learn how to read data from a file.

3 SE15: Streams and File I/O30–3 Lecture Outline Overview of Streams and File I/O. Text file I/O The File Class Savitch Chapter 9

4 SE15: Streams and File I/O30–4 What is a Stream? A stream is a flow of data characters, numbers or bytes of binary digits Input Stream – data flows into your program (files, keyboard) Output Stream data flows out (Screen, files) Java implements streams as objects of stream classes Examples we have seen so far?

5 SE15: Streams and File I/O30–5 Text files vs Binary files All data in any file are stored as binary digits (0’s & 1’s) Text files Can think of a files contents as consisting of a sequence of characters. Have streams and methods that make the binary digits look like characters to your program or editor. Usually appear the same on all computers. Human readable. Binary files Contents is handled as a sequences of binary digits. More efficient. Implementation differs between computers.

6 SE15: Streams and File I/O30–6 Text File I/O 3 steps are required 1. Open a file (connects a file to a stream) 2. Write to the file 3. Close the file Classes we shall look at: PrintWriter class BufferedReader class File class

7 SE15: Streams and File I/O30–7 Opening a text file for writing with PrintWriter First declare a variable of type PrintWriter e.g.. PrintWriter outputStream = null; (this is the name of the stream) Next construct the outputStream object by calling the constructor for the PrintWriter class The PrintWriter class has no constructor that takes a file name as its argument so we use the class FileOutputStream to create a stream that can be used as an argument to the PrintWriter constructor e.g. new FileOutputStream(“out.txt”) outputStream = new PrintWriter(new FileOutputStream(“out.txt”));

8 SE15: Streams and File I/O30–8 FileNotFoundException When you open a file for writing the constructor might throw an exception FileNotFoundException It means the file could not be created, such as when the filename was already in use for a directory name You should therefore catch the exception in a catch block or throw it PrintWriter outputStream = null; try { outputStream = new PrintWriter(new FileOutputStream(“out.txt”)); } catch(FileNotFoundException e) { System.out.println(“Error openning file”); System.exit(0); }

9 SE15: Streams and File I/O30–9 Writing to a text file with PrintWriter PrintWriter has a method println that behaves like the System.out.println method but the output goes to a text file outputStream.println(“this line will be written to a file”); System.out.println(“this line will be displayed on the screen”);

10 SE15: Streams and File I/O30–10 Closing a file with PrintWriter When your program has finished writing to a file, it should close the stream connected to the file. outputStream.close(); Why bother closing a file? Java will close any open files for you if your program terminates normally. If an error occurs the files may not be closed and the files can be damaged. If you open a file for writing you must close it before it can be read.

11 SE15: Streams and File I/O30–11 Overwriting and appending files Connecting to a stream using outputStream = new PrintWriter(new FileOutputStream(“out.txt”)); Will always produce an empty file even if a file of that name already exists!! If you wish to add data to a file (appending a file) you must use outputStream = new PrintWriter(new FileOutputStream(“out.txt”,true));

12 SE15: Streams and File I/O30–12 Opening a text file for input with BufferedReader a) declare a stream variable e.g. BufferedReader inputStream = null; b) construct the stream object As with PrintWriter there is no constructor that takes a file name as an argument therefore we need another class FileReader to help with opening the file. inputStream = new BufferedReader( new FileReader(“data.txt”)); You should also catch the FileNotFoundException.

13 SE15: Streams and File I/O30–13 Reading data from the file with BufferedReader The method readLine reads from a text file just as the nextLine method of the class Scanner reads a line of text from the keyboard. String line = inputStream.readLine(); It does not have methods such as nextInt and nextDouble. readLine may throw an IOException When readline tries to read beyond the end of the file it returns null When your program has finished reading from the file, it should close the stream connected to the file. inputStream.close();

14 SE15: Streams and File I/O30–14 Using path names A typical UNIX path name is /user/smith/homework1/data.txt To create a FileReader for this file you use new FileReader(“/user/smith/homework1/data.txt”) Windows uses \ instead of / in path names A typical Windows path name is D:\homework1\data.txt To create a FileReader for this file you use new FileReader(“D:\\homework1\\data.txt”);

15 SE15: Streams and File I/O30–15 Summary Looked at the concept of Streams Learnt how to write text files using the PrintWriter class Learnt how to read text files using the Buffered Reader class

16 SE15: Streams and File I/O30–16 Follow-up Work Exercise 7


Download ppt "Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming."

Similar presentations


Ads by Google