File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Looping through a text file with an iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Looping through a text file with an iterator" Watch "Looping through a text file with an iterator" New topic
Author

Looping through a text file with an iterator

Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 170

Just thought somebody might find this interesting/useful.
I wanted to use the new 'for' syntax to loop through all the lines in a text file; something I find myself doing quite often.

A simple example:


It required making two classes: FileLineIterable, and FileLineIterator.
You might find this useful as an example for implementing your own iterator class.





 
 
subject: Looping through a text file with an iterator
 
Similar Threads
Syntax error in INSERT INTO statement
Using Swing to display an array from text file issues
save properties file without affecting existing comments.
Java: Convert a binary file to "text" and back again.
Need to read and write data from file.