Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.

Similar presentations


Presentation on theme: "Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis."— Presentation transcript:

1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 5, 10: Files

2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-2 Files File handling is an important aspect of many programs Chapter 5 covers: –Reading from files Chapter 10 covers: –Writing to files

3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-3 Outline Reading from files Writing to files

4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-4 Iterators An iterator is an object that allows you to process a collection of items one at a time It lets you step through each item in turn and process it as needed An iterator object has a hasNext method that returns true if there is at least one more item to process The next method returns the next item Iterator objects are defined using the Iterator interface, which is discussed further in Chapter 6

5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-5 Iterators Several classes in the Java standard class library are iterators The Scanner class is an iterator –the hasNext method returns true if there is more data to be scanned –the next method returns the next scanned token as a string The Scanner class also has variations on the hasNext method for specific data types (such as hasNextInt )

6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-6 Iterators The fact that a Scanner is an iterator is particularly helpful when reading input from a file Suppose we wanted to read and process a list of URLs stored in a file One scanner can be set up to read each line of the input until the end of the file is encountered Another scanner can be set up for each URL to process each part of the path See URLDissector.javaURLDissector.java

7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-7 Converting Text Often the output from a file is a String. Remember you can use –Integer.parseInt(String) –Float.parseFloat(String) –Double.parseDouble(String) To convert the String into whatever you need

8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-8 Outline Reading from files Writing to files

9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-9 I/O Exceptions Let's examine issues related to exceptions and I/O A stream is a sequence of bytes that flow from a source to a destination In a program, we read information from an input stream and write information to an output stream A program can manage multiple streams simultaneously

10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-10 Standard I/O There are three standard I/O streams: –standard output – defined by System.out –standard input – defined by System.in –standard error – defined by System.err We use System.out when we execute println statements System.out and System.err typically represent a particular window on the monitor screen System.in typically represents keyboard input, which we've used many times with Scanner objects

11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-11 The IOException Class Operations performed by some I/O classes may throw an IOException –A file might not exist –Even if the file exists, a program may not be able to find it –The file might not contain the kind of data we expect An IOException is a checked exception

12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-12 Writing Text Files In Chapter 5 we explored the use of the Scanner class to read input from a text file Let's now examine other classes that let us write data to a text file The FileWriter class represents a text output file, but with minimal support for manipulating data Therefore, we also rely on PrintStream objects, which have print and println methods defined for them

13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10-13 Writing Text Files Finally, we'll also use the PrintWriter class for advanced internationalization and error checking We build the class that represents the output file by combining these classes appropriately See TestData.java TestData.java Output streams should be closed explicitl


Download ppt "Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis."

Similar presentations


Ads by Google