This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Send lines of a file to something such as an arrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Send lines of a file to something such as an arrayList" Watch "Send lines of a file to something such as an arrayList" New topic
Author

Send lines of a file to something such as an arrayList

M Strasser
Greenhorn

Joined: Mar 30, 2004
Posts: 1
I have a text file that I want to go through and put specific entries in to an arrayList (or any other list). I want to select a line by matching up a piece of string. For example, I want to take all lines that have "B204" somewhere in them and put them into an arrayList. I already know how to read the file:
BufferedReader textline = new BufferedReader(new FileReader("textfile.txt"));
what do I do next?
Thanks
Micah
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Create a new ArrayList.
loop reading lines.
check the lines for your pattern (Pattern, Matcher, String.matches(...) in javadocs).
Add matching lines to the list.
handle exceptions.
close file.
done.


http://home.arcor.de/hirnstrom/bewerbung
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Micah, welcome to the Ranch!
We don't have a lot of rules, but we do have our Naming Policy. Specifically, last names should not be initials. First and middle names are fine, but last names should be "realistic" looking. Doesn't have to be your real last name (though we prefer that), but something more than just a letter.
Head over to your profile and change your display name please to conform to our policy.
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Send lines of a file to something such as an arrayList
 
Similar Threads
Write File to Harddrive
Can't read a file
Returning array To Method
Array vs java.util.ArrayList
tricky question