Meaningless Drivel is fun!*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 173

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.





 
Don't get me started about those stupid light bulbs.
 
subject: Looping through a text file with an iterator
 
Similar Threads
save properties file without affecting existing comments.
Java: Convert a binary file to "text" and back again.
Using Swing to display an array from text file issues
Syntax error in INSERT INTO statement
Need to read and write data from file.