Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

how to read lines from a file

Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a txt file.
i have to read the content of the sample txt file by line by line...
Please let me know what modifications i have to do in the following code..
class SampelRead
public static void main(String[] args)
BufferedReader br_reader = new BufferedReader(new FileReader("sample.txt"));
String strin = br_reader.readLine();
System.out.println("Line ::"+strin);

}catch(IOException objIOException)
[ August 07, 2003: Message edited by: sravan reddy ]
author and iconoclast
Posts: 24204
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As is, it should basically work and display the first line of the file (you'd want to add "import*;" at the top.) I do have plenty of critiques, though, and then finally I'll tell you how to read all the lines.
First, you've forgotten to close the file. After you're through working with a file, you have to close it so the OS can reclaim the resources associated with keeping it open. You can simply add "br_reader.close();" as the last line at the end of the try block.
Second, NEVER write an empty catch block like the one you have here. If an exception is thrown, and your catch block does nothing, then your program will simply not work, and you won't know why. You'll spend hours trying to figure out what happened. If instead your catch block contained a trivial "objIOException.printStackTrace();" then you would have gotten a nice explanation of the problem, fixed it, and moved on.
Third (and now I'm starting to get picky) your variables names are inconsistent and not especially well chosen. All three of the names include some form of the data type, which is rarely done in Java, as Java (unlike C) is strongly typed. But each of the names uses a different convention to encode the type! br_reader uses initials and an underscore, "strin" uses an oddly truncated version of the class name, and objIOException uses capitalization and puts the type at the end. Spending a moment to thing of appropriate variable names will make your programs easier to understand. I'd probably use "reader", "line", and "error" for these three, respectively.
Finally, how do you read all of the lines? Using a "while" loop would be one way. The body of your try{} block could look like this (BufferedReader returns "null" on end-of-file):
These are the worst of times and these are the best of times. And this is the best tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic