1 / 31

Lecture 11 File I/O

Lecture 11 File I/O. Richard Gesick. Topics. The java.io Package Parsing a String using Scanner Reading Text Files Using Scanner Writing and Appending to Text Files Reading Structured Text Files Reading and Writing Objects to a File. Selected Input Classes in the java.io Package.

kenda
Download Presentation

Lecture 11 File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 11File I/O Richard Gesick

  2. Topics • The java.io Package • Parsing a String using Scanner • Reading Text Files Using Scanner • Writing and Appending to Text Files • Reading Structured Text Files • Reading and Writing Objects to a File

  3. Selected Input Classes in the java.io Package

  4. Hierarchy for Input Classes

  5. Selected java.io Output Classes

  6. Hierarchy for Output Classes

  7. Reading Text Files Earlier, we read a text file by associating a Scanner object with a File object: Scanner file = new Scanner( new File( “filename.txt” ) ); We added the throws IOException clause to main to avoid handling exceptions that may occur. public static void main( String [] args ) throws IOException But when the file wasn’t found, a FileNotFoundException occurred; the user was given an unfriendly message; and the program terminated. Now that we know how to catch exceptions, we can intercept the FileNotFoundException and give the user a meaningful message.

  8. Opening and Closing an InputStream • When we construct an input stream or output stream object, the JVM associates the file name, standard input stream, or standard output stream with our object. This is opening the file. • When we are finished with a file, we optionally call the close method to release the resources associated with the file.

  9. Opening and Closing Standard Streams The standard input stream (System.in), the standard output stream (System.out), and the standard error stream (System.err) are open when the program begins.They are intended to stay open and should not be closed.

  10. SOFTWARE ENGINEERING TIP Calling the close method is optional. When the program finishes executing, all the resources of any unclosed files are released. It is good practice to call the close method, however, especially if you will be opening a number of files (or opening the same file multiple times.) Do not close the standardinput, output, or error devices, however. They are intended to remain open.

  11. Exceptions While Reading from a File We can catch this exception: FileNotFoundExceptionthrown by the Scanner constructor if the filename is not found when opening the file We do not expect these exceptions to occur, so we will catch them as subclasses of IOException, and print the stack trace. InputMismatchExceptionif the input does not match the expected data type. (The next method does not throw this exception, so we don’t need to catch this exception). NoSuchElementExceptionif we attempt to read beyond the end of the file. IllegalStateExceptionif we attempt to read after calling the close method.

  12. Writing to Text Files • Several situations can exist: • the file does not exist • the file exists and we want to replace the current contents • the file exists and we want to append to the current contents • We specify whether we want to replace the contents or append to the current contents when we construct our FileOutputStream object.

  13. Constructors for Writing to Text Files

  14. Methods of the PrintWriter Class

  15. Reading Structured Text Files Some text files are organized into lines that represent a record — a set of data values containing information about an item. • The data values are separated by one or more delimiters; that is, a special character or characters that separate one value from the next. • As we read the file, we need to parse each line; that is, separate the line into the individual data values called tokens.

  16. Example An airline company could store data in a file where each line represents a flight segment containing the following data: • flight number • origin airport • destination airport • number of passengers • average ticket price Such a file could contain the following data: AA123,BWI,SFO,235,239.5 AA200,BOS,JFK,150,89.3 … In this case, the delimiter is a comma.

  17. Using Scanner to Parse Strings The Scanner constructor below accepts a String. We can use any of the Scanner next methods to read tokens from the String. We can use any of the Scanner hasNextmethods to determine whether more tokens are available to be read. The default delimiters are the white space characters (space, newline, tab, etc.).

  18. Using Scanner to Parse Strings To specify different delimiters, call useDelimitermethod: pattern represents a regular expression against which to match sequences of characters using standard characters as well as meta-characters, which have special meanings. We can specify a delimiter consisting of a single character or multiple specific characters as a simple String argument.

  19. Example The file flight.txt contains the following comma-separated flight data on each line: flight number, origin airport, destination airport, number of passengers, average ticket price TheFlightRecord class defines instance variables for each flight data value The ReadFlights class reads data from flights.txt, instantiates FlightRecord objects, and adds them to an ArrayList.

  20. Reading and Writing Objects Java also supports writing objects to a file and reading them as objects. This is convenient for two reasons:  • We can write these objects directly to a file without having to convert the objects to primitive data types or Strings. • We can read the objects directly from a file, without having to read Strings and convert these Strings to primitive data types in order to instantiate objects.   To read objects from a file, the objects must have been written to that file as objects.

  21. Writing Objects to a File To write an object to a file, its class must implement the Serializable interface, which indicates that: • the object can be converted to a byte stream to be written to a file • that byte stream can be converted back into a copy of the object when read from the file. TheSerializable interface has no methods to implement. All we need to do is: • import thejava.io.Serializable interface • add implementsSerializable to the class header

  22. Serializable • Objects that are written to an object stream must belong to a class that implements the Serializable interface. class BankAccount implements Serializable{ . . . }

  23. Serializable • Serialization: process of saving objects to a stream • Each object is assigned a serial number on the stream • If the same object is saved twice, only serial number is written out the second time • When reading, duplicate serial numbers are restored as references to the same object

  24. The ObjectOutputStream Class The ObjectOutputStream class, coupled with the FileOutputStream class, provide the functionality to write objects to a file. The ObjectOutputStream class provides a convenient way to write objects to a file. • Its writeObject method takes one argument: the object to be written.

  25. Constructors for Writing Objects

  26. The writeObject Method

  27. Omitting Data from the File The writeObjectmethod does not write any object fields declared to be static or transient. You can declare a field as transient if you can easily reproduce its value or if its value is 0. Syntax to declare a field as transient: accessModifier transient dataTypefieldName Example: private transient double totalRevenue;

  28. SOFTWARE ENGINEERING TIP To save disk space when writing to an object file, declare the class's fields as static or transient, where appropriate.

  29. Reading Objects from a File The ObjectInputStream class, coupled with FileInputStream, provide the functionality to read objects from a file. The readObject method of the ObjectInputStream class is designed to read objects from a file. Because the readObjectmethod returns a generic Object,we must type cast the returned object to the appropriate class. When the end of the file is reached, the readObject method throws an EOFException, so we detect the end of the file when we catch that exception.

  30. Constructors for Reading Objects

  31. The readObject Method -- we detect reaching the end of the file in the catch block for EOFException • we use a finally block to close the file.

More Related