Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 2 Classes you are responsible for File FileInputStream, FileOutputStream BufferedReader, PrintWriter, PrintStream ObjectInputStream, ObjectOutputStream We'll cover JFileChooser with Chapter 14.

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 3 The File Class We can use a File object (from java.io) to represent a file. File inFile = new File(“sample.dat”); File inFile = new File (“C:/SamplePrograms/test.dat”); Opens the file sample.dat in the current directory. Opens the file test.dat in the directory C:\SamplePrograms using the generic file separator / and providing the full pathname.

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 4 Some File Methods if ( inFile.exists( ) ) { if ( inFile.isFile() ) { File directory = new File("C:/JavaPrograms/Ch12"); String filename[] = directory.list(); for (int i = 0; i < filename.length; i++) { System.out.println(filename[i]); } To see if inFile is associated to a real file correctly. To see if inFile is associated to a file or not. If false, it is a directory. List the name of all files in the directory C:\JavaProjects\Ch12

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 5 I/O Streams A common approach to input and output in a program is to view it as a stream of data. A stream is a sequence of data items. 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. –An output stream has a destination to which the data items are going. Java has a large collection of classes for I/O –Look at the java.io package in the API –Many of these classes are related by inheritance –The classes for high-level I/O generally have a lower-level stream inside of them.

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 6 File I/O There are a number of I/O classes available for working with files –To read data from or write data to a file, we must create one of the Java stream objects and attach it to a file. All of the I/O classes used with files have multiple constructors –You can create them from a File object If you need to supply a Path as well as a filename, this is probably the best approach –You can create them from a String containing the name of the file

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 7 Streams for Low-Level File I/O At the lowest level, we have InputStream and OutputStream which operate on bytes (8 bits) –System.in is an InputStream FileOutputStream and FileInputStream are two stream objects that facilitate file access. –FileOutputStream allows us to output a sequence of bytes; values of data type byte. –FileInputStream allows us to read in an array of bytes. We don't generally want to work with information in byte format

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 8 High-Level I/O At a higher level, we have several choices –Binary data is stored in a file in the same form as it is stored in memory Efficient use of space Need a program to read it –ASCII files store data as text All data is represented as a String Text files are human readable –Object files This is a binary format Minimal code needed to output entire object

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 9 Textfile Input and Output To output data as a string to file, we use a PrintWriter object For input from a textfile, we have a couple of choices –We can use FileReader and BufferedReader classes –We can also use the Scanner class

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 10 Sample Textfile Output import java.io.*; class Ch12TestPrintWriter { public static void main (String[] args) throws IOException { //set up file and stream File outFile = new File("sample3.data"); FileOutputStream outFileStream = new FileOutputStream(outFile); PrintWriter outStream = new PrintWriter(outFileStream); //write values of primitive data types to the stream outStream.println(987654321); outStream.println("Hello, world."); outStream.println(true); //output done, so close the stream outStream.close(); }

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 11 Sample Textfile Input import java.io.*; class Ch12TestBufferedReader { public static void main (String[] args) throws IOException { //set up file and stream File inFile = new File("sample3.data"); FileReader fileReader = new FileReader(inFile); BufferedReader bufReader = new BufferedReader(fileReader); String str; str = bufReader.readLine(); int i = Integer.parseInt(str); //similar process for other data types bufReader.close(); }

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 12 Sample Textfile Input with Scanner import java.io.*; class Ch12TestScanner { public static void main (String[] args) throws IOException { //open the Scanner Scanner scanner = new Scanner(new File("sample3.data")); //get integer int i = scanner.nextInt(); //similar process for other data types scanner.close(); }

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 13 Alternate Constructors For output FileOutputStream outFileStream = new FileOutputStream("sample.dat"); We can also go directly to a FileWriter FileWriter outFileWriter = new FileWriter("sample.dat"); For input FileInputStream inFileStream = new FileInputStream("sample.dat")); Using a BufferedReader BufferedReader bufReader = new BufferedReader( inFileStream); Using a Scanner Scanner scanner = new Scanner( inFileStream);

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 14 Object File I/O It is possible to store objects just as easily as you store primitive data values. We use ObjectOutputStream and ObjectInputStream to save to and load objects from a file. To save objects from a given class, the class declaration must include the phrase implements Serializable. For example, class Person implements Serializable {... }

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 15 Saving Objects FileoutFile = new File("objects.data"); FileOutputStream outFileStream = new FileOutputStream(outFile); ObjectOutputStream outObjectStream = new ObjectOutputStream(outFileStream); Person person = new Person("Mr. Espresso", 20, 'M'); outObjectStream.writeObject( person ); account1= new Account(); bank1 = new Bank(); outObjectStream.writeObject( account1 ); outObjectStream.writeObject( bank1 ); Could save objects from the different classes.

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 16 Reading Objects FileinFile = new File("objects.data"); FileInputStream inFileStream = new FileInputStream(inFile); ObjectInputStream inObjectStream = new ObjectInputStream(inFileStream); Person person = (Person) inObjectStream.readObject( ); Account account1 = (Account) inObjectStream.readObject( ); Bank bank1 = (Bank) inObjectStream.readObject( ); Must read in the correct order. Must type cast to the correct object type.

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 17 Saving and Loading Arrays Instead of processing array elements individually, it is possible to save and load the whole array at once. Person[] people = new Person[ N ]; //assume N already has a value //build the people array... //save the array outObjectStream.writeObject ( people ); //read the array Person[ ] people = (Person[]) inObjectStream.readObject( );


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output."

Similar presentations


Ads by Google