944,069 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 19363
  • Java RSS
Jun 8th, 2007
0

How can I delete empty lines from file?

Expand Post »
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

Java Syntax (Toggle Plain Text)
  1. FileInputStream fstream = new FileInputStream("input.txt");
  2. DataInputStream in = new DataInputStream(fstream);
  3. BufferedReader br = new BufferedReader (new InputStreamReader(in));
  4.  
  5. String strLine;
  6. while(br.readline()!=null)
  7. {
  8. //....
  9. }

But the program stops in the first null line.

Any ideas ?

Thanks
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
katerinaaa is offline Offline
11 posts
since May 2007
Jun 8th, 2007
0

Re: How can I delete empty lines from file?

Java Syntax (Toggle Plain Text)
  1. BufferedReader br = new BufferedReader(new FileReader("c:/test.txt"));
  2. String strLine;
  3. while((strLine=br.readLine())!=null) {
  4. if (strLine.length()>0) System.out.println(strLine);
  5. }
Of course, you will probably want to write those lines back out to a file instead of printing them to System.out.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 9th, 2007
0

Re: How can I delete empty lines from file?

> 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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
May 2nd, 2008
0

Re: How can I delete empty lines from file?

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

java Syntax (Toggle Plain Text)
  1. // command line parameter
  2. FileInputStream fstream = new FileInputStream("myfile.txt");
  3. // Get the object of DataInputStream
  4. DataInputStream in = new DataInputStream(fstream);
  5. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  6. while ((line = br.readLine()) != null && !"".equals(line = br.readLine().trim())) {
  7. ......
  8. }
Last edited by addiakogiannis; May 2nd, 2008 at 7:39 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
addiakogiannis is offline Offline
1 posts
since May 2008
May 2nd, 2008
0

Re: How can I delete empty lines from file?

This thread is almost a year old. I doubt they still need help on it.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
May 2nd, 2008
0

Re: How can I delete empty lines from file?

Not to mention that this while condition is just plain wrong
Java Syntax (Toggle Plain Text)
  1. while ((line = br.readLine()) != null && !"".equals(line = br.readLine().trim())) {
  2. ......
  3. }
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.
Last edited by Ezzaral; May 2nd, 2008 at 1:06 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
May 6th, 2008
0

just try!

//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";
}
Reputation Points: 9
Solved Threads: 1
Light Poster
alpe gulay is offline Offline
45 posts
since May 2008
May 6th, 2008
0

Re: just try!

Click to Expand / Collapse  Quote originally posted by alpe gulay ...
//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.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
May 6th, 2008
0

Re: How can I delete empty lines from file?

.,sory poh!!!
i'm still beginners in this site..!
I took crelessness with this one...
hehehehe!!!dont get mad guys!
Reputation Points: 9
Solved Threads: 1
Light Poster
alpe gulay is offline Offline
45 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how can we delete a directory in java???
Next Thread in Java Forum Timeline: How to pass a keyevent





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC