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

Recommended Answers

All 8 Replies

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.

> 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.

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()))   {
......
}

This thread is almost a year old. I doubt they still need help on it.

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.

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

start quote:

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

end quote.

Read before you post. We already stated the question was asked one year ago.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.