aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in reading empty line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in reading empty line" Watch "Problem in reading empty line" New topic
Author

Problem in reading empty line

babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
HI ,
I want to read the file and print the contents of file.
For that I wrote :

File file = new File("test_file");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while( (line=br.readLine()) != null )
{
line = line.trim();
System.out.println( line );
}

Above is working if there is no blank lines in the file.
If file is like below :
test1
test2

test3
End of file.

Then it is printing first two lines only.Because 3rd line is empty.
How to handle this situation ? Is readLine() takes that empty line as null ?

Regards,
Sharath
babu sharath
Ranch Hand

Joined: Jul 06, 2006
Posts: 42
HI ,
Sorry I didn't wrote the complete code earlier :

File file = new File("test_file");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while( (line=br.readLine()) != null )
{
line = line.trim();
System.out.println( line );
if (line.charAt(0)=='#')
{
continue;
}
}
( When a line starts with a # I want to ignore that )
In above senario , file is read upto empty line and next lines were not read
what is the problem with above code ?

Sharath
prashanth kumar
Ranch Hand

Joined: Mar 22, 2004
Posts: 162
I remember using ready() method when i wrote code long time back..
So try to put an AND condition in your while loop using ready() method..There may be some better ways as well...


SCJP1.5(86%)<br />SCWCD1.4(95%)<br />SCBCD1.3(92%)<br />IBM 252
Jae Stryker
Greenhorn

Joined: Oct 31, 2006
Posts: 21



Hi could your problem be within the while loop - i.e. you are looping until it finds a null line (which a blank line could be), would it not be better to use an end of file marker - This is only a suggestion of course.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023


readLine() just returns "", the empty string, for an empty line.

[ October 31, 2006: Message edited by: wise owen ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Following on from what "wise owen" writes, I guess that the original poster is throwing away exceptions - correct?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
Hi Babu Sarath,

Try using StringReader class (JDK 1.5 API) instead of BufferedReader. Use the read method, which will read data from the file till EOF is reached.

Hope it helps.

Regards

Nikhil Bansal


ban$al
 
Don't get me started about those stupid light bulbs.
 
subject: Problem in reading empty line