How can I delete empty lines from file?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 11
Reputation: katerinaaa is an unknown quantity at this point 
Solved Threads: 0
katerinaaa katerinaaa is offline Offline
Newbie Poster

How can I delete empty lines from file?

 
0
  #1
Jun 8th, 2007
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

  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How can I delete empty lines from file?

 
0
  #2
Jun 8th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: How can I delete empty lines from file?

 
0
  #3
Jun 9th, 2007
> 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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: addiakogiannis is an unknown quantity at this point 
Solved Threads: 0
addiakogiannis addiakogiannis is offline Offline
Newbie Poster

Re: How can I delete empty lines from file?

 
0
  #4
May 2nd, 2008
So you want to skip the empty lines correct?
Here you go....

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: How can I delete empty lines from file?

 
0
  #5
May 2nd, 2008
This thread is almost a year old. I doubt they still need help on it.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How can I delete empty lines from file?

 
0
  #6
May 2nd, 2008
Not to mention that this while condition is just plain wrong
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: alpe gulay is an unknown quantity at this point 
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

just try!

 
0
  #7
May 6th, 2008
//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";
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

Re: just try!

 
0
  #8
May 6th, 2008
Originally Posted by alpe gulay View Post
//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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 45
Reputation: alpe gulay is an unknown quantity at this point 
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

Re: How can I delete empty lines from file?

 
0
  #9
May 6th, 2008
.,sory poh!!!
i'm still beginners in this site..!
I took crelessness with this one...
hehehehe!!!dont get mad guys!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 9511 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC