Presentation is loading. Please wait.

Presentation is loading. Please wait.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.

Similar presentations


Presentation on theme: "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output."— Presentation transcript:

1 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output Object Input and Output

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Files  Program data that is stored in variables ceases to exist when the program terminates.  Files provide a way of storing data after the program terminates.  There are two basic ways to store data in files text data is human readable, system independent binary data has the same format as it would in memory

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. File I/O  Once a file is opened by associating a File object to it, we can access the data in the file.  To read data from or write data to a file, we must create one of the Java stream objects and attach it to the file.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Streams  A stream is a sequence of data items, usually 8-bit bytes.  Java has two types of streams: an input stream and an output stream.  An input stream has a source form which the data items come, and an output stream has a destination to which the data items are going.

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. FileStreams  FileOutputStream and FileInputStream are two stream objects that facilitate file access. Used for byte data  FileReader and FileWriter are used for input and output of text data

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. File Input and Output  The action of saving, or writing, data to a file is called file output.  The action of reading data from a file is called file input.  To work with files, we need to make an association between an object in the program and a file on the disk. When a valid association is established, we say a file is opened. A file must be opened before we can do any input and output to the file.

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Text Files  Data stored as text is easy for humans to deal with.  All data is converted to string data.  A file whose contents are stored in ASCII format is called a text file.  A text file is what you create with vi.  Text data is what we get from the keyboard and send to the console.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. I/O Classes  Keyboard input and console output

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. File InputStream  Two ways to create a FileInputStream Give the constructor the name of the file FileInputStream fin = new FileInputStream(“sample.data”); Create the FileInputStream and then open it FileInputStream fin = new FileInputStream(); fin.open( “sample.data”);  From the FileInputStream, we can create either a Scanner or a BufferedReader Create in the same way as for using System.in

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Text Input using a BufferedReader  To read data from a text file, we use the FileReader and BufferedReader objects. We first associate a BufferedReader object to a file. Then we read data, using the readLine method of BufferedReader. Finally, we convert the String to a primitive data type as necessary.  This is the same process we used for reading from a JOptionPane

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. FileReader Class  We can use the FileReader class to reduce the number of steps needed to create a BufferedReader FileReader fin = new FileReader("file.dat"); BufferedReader in = new BufferedReader( fin);

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Text Output  PrintWriter is an object we use to generate a textfile.  PrintWriter supports only two output methods: print println (for print line)  An argument to the methods may be any primitive data type.  The methods convert the parameter to string and output this string value.  These should look familiar - System.out is a PrintStream

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. PrintWriter Objects  The constructor of PrintWriter requires a Writer object as its argument. FileWriter = new FileWriter( "test.dat"); PrintWriter out = new PrintWriter( fout);

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. IOExceptions for File I/O  The methods that link a file to a Stream or Reader or Writer will all throw an IOException if they fail. For reading, failure means the file wasn't found For writing, you would get an error if a write-protected file of the same name exists.

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object I/O  We can also store objects just as we can store primitive data values.  To write objects to a file, we use ObjectOutputStream.  To read objects from a file, we use ObjectInputStream.

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object I/O  In this example, we will write Person objects to a file.  The first step is to modify the Person class definition to allow ObjectOutputStream and ObjectInputStream to perform object I/O.

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Serializable Interface  We modify the definition by adding the phrase implements Serializable to it. import java.io.*; class Person implements Serializable { //the rest is the same }  There are no methods for us to define in the implementation class.

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object Output  To save objects to a file, we first create an ObjectOutputStream object: File outFile = new File(“objects.dat”); FileOutputStream outFileStream = new FileOutputStream(outFile); ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStre am);

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object Output  To save a Person object, we execute Person person = new Person(“Mr. Espresso”, 20, ‘M’); outObjectStream.writeObject(person) ;

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object Output  Different types of objects may be saved to a single file.  We can also mix objects and primitive data type values in the same file.  If a file contains objects from different classes, they must be read in the correct order and the matching type casting must be applied.

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Object Input  To read objects from a file, we use FileInputStream and ObjectInputStream.  We use the method readObject to read an object.  Because we can store any types of objects to a single file, we must type cast the object read from the file.  The readObject method can throw a ClassNotFoundException (wrong type casting) in addition to an IOException. Either exception may be caught or propagated.


Download ppt "©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output."

Similar presentations


Ads by Google