File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Best way to open a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Best way to open a text file" Watch "Best way to open a text file" New topic

Best way to open a text file

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 832
As the subject goes, what's the optimum way to open a text file with multiple, varying length lines for processing. Basically the lines in the file have values that are tab-delimited and I want to read the values in and assign them to different fields on a HTML form. Thanks.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look through the JavaDoc for FileReader and BufferedReader to read a file one line at a time. Look at String.split (JDK 1.4+) or StringTokenizer to break up the tab delimited fields. If you're feeling adventurous and you know some RegEx, look at Scanner (JDK 5+) to do both at once. Let us know how it goes!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Actually if you're using JDK 5, Scanner is a good choice even if you don't know regex (yet). You can just create one and use the nextLine() method to read lines, much like you would use readLine() in a BufferedReader. But Scanner gives you a lot more options than BufferedReader does.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Best way to open a text file
It's not a secret anymore!