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 Scanner, 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: input streams and output streams. –Input stream has source from which the data comes. –Output stream has destination to which the data goes. 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 Many I/O classes for working with files –first create a stream object and attach it to a file. All I/O classes 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 usually 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 text to a file, we use a PrintStream or a PrintWriter object –Both have the print and println methods –System.out is a PrintStream For input from a text file, we have a couple of choices –We can use the Scanner class –We can use FileReader and BufferedReader classes

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 10 Sample Textfile Output import java.io.*; public class TestPrintWriter { public static void main (String[] args) throws IOException { //set up file and stream FileOutputStream outFileStream = new FileOutputStream("sample3.data"); 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 with Scanner import java.io.*; class Ch12TestScanner { public static void main (String[] args) throws IOException { //open the Scanner Scanner scanner = new Scanner( new FileInputStream("sample3.data")); //get integer int i = scanner.nextInt(); //similar process for other data types scanner.close(); }

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 12 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 {... }

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 13 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.

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 14 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.

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 15 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