Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to open a text file

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Rob
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic