• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How is file read by lines ?

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..............
try{
File f1 = new File("C:\JavaFiles\testFile.txt");
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
String line;
While((line=br.readLine()) !!= null){
System.out.println(line);
}....................

In this code when we read a text file in File object, does file not get converted in a single object ? If its becoming a single object, how does a that object can be read line by line ? Does it not become a monolithic object. If all the new line characters are wrapped within object, then FileReader or BufferedReader will read object by object and not line by line within an object.

Please explain.

Thanks
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The File object does not contain the contents of the file. It is merely a descriptor for the file.

The file contents are brought into memory by the reader -- in this case, line by line as defined by the reader.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should declare the File before the try block. You will want to send an error message if the file fails to open, so you will want this object to be in-scope for the catch block.
reply
    Bookmark Topic Watch Topic
  • New Topic