Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.

Similar presentations


Presentation on theme: "Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns."— Presentation transcript:

1 Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns true if there is more data to scan  The next returns the next scanned token as a String The fact that a Scanner is an iterator is particularly helpful when reading input from a file Let us consider an example where We want to read and process a list of URLs stored in a file

2 URLDissector.java The program reads the URL from a file (urls.inp) And dissect them to show the various parts of the path One scanner can be setup To read each line of the input Until the end of the file is encountered Another scanner can be setup for each URL To process each part of the path

3 Writing Text Files Let's now examine 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

4 Writing Text Files Finally, we'll use the PrintWriter class We build the class that represents the output file by combining these classes appropriately See TestData.java (page 547) TestData.java Output streams should be closed explicitly


Download ppt "Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns."

Similar presentations


Ads by Google