BufferedReader br = new BufferedReader(new FileReader("c:/test.txt"));
String strLine;
while((strLine=br.readLine())!=null) {
if (strLine.length()>0) System.out.println(strLine);
}
Of course, you will probably want to write those lines back out to a file instead of printing them to System.out.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
> But the program stops in the first null line.
null != empty_line
A null returned signifies that a end of file has been reached and has got nothing to do with empty lines. Inside your loop, just check to see if str.trim().length() != 0 and write that line to the target file.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
This thread is almost a year old. I doubt they still need help on it.
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
Not to mention that this while condition is just plain wrong
while ((line = br.readLine()) != null && !"".equals(line = br.readLine().trim())) {
......
}
It will read two lines and test them separately. One will be tested for null and the second for an empty string. I don't think the goal is to process every other line in the file.
And it's a confusing mess to read. Try for clarity over being clever just to minimize the lines of code.
Ezzaral
Posting Genius
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
//after declaring your filereader just put up this condition
BufferedReader reader = new BufferedReader(new FileReader("--your file here--"));
String line="";
String out="";
while((line=reader.readLine())!=null)
{
if (line.length()>0)
out+=line+"\n";
}
Read before you post. We already stated the question was asked one year ago.
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53