954,202 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How can I delete empty lines from file?

Hi,
I would like to ask if anyone knows how can I delete empty lines from a file.

I thought that I have to crate a loop like

FileInputStream fstream = new FileInputStream("input.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader (new InputStreamReader(in));
    
String strLine;
while(br.readline()!=null)
{
//....
}


But the program stops in the first null line.

Any ideas ?

Thanks

katerinaaa
Newbie Poster
11 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 
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
Moderator
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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 

So you want to skip the empty lines correct?
Here you go....

// command line parameter
FileInputStream fstream = new FileInputStream("myfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null && !"".equals(line = br.readLine().trim()))   {
......
}
addiakogiannis
Newbie Poster
1 post since May 2008
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
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";
}

alpe gulay
Light Poster
45 posts since May 2008
Reputation Points: 9
Solved Threads: 1
 
//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
 

.,sory poh!!!
i'm still beginners in this site..!
I took crelessness with this one...
hehehehe!!!dont get mad guys!

alpe gulay
Light Poster
45 posts since May 2008
Reputation Points: 9
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You