Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How is file read by lines ?  RSS feed

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
John M Morrison
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!