| | |
How can I delete empty lines from file?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2007
Posts: 11
Reputation:
Solved Threads: 0
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
But the program stops in the first null line.
Any ideas ?
Thanks
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)
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
Java Syntax (Toggle Plain Text)
BufferedReader br = new BufferedReader(new FileReader("c:/test.txt")); String strLine; while((strLine=br.readLine())!=null) { if (strLine.length()>0) System.out.println(strLine); }
> But the program stops in the first null 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
null != empty_lineA 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
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
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
So you want to skip the empty lines correct?
Here you go....
Here you go....
java Syntax (Toggle Plain Text)
// 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())) { ...... }
Last edited by addiakogiannis; May 2nd, 2008 at 7:39 am.
Not to mention that this while condition is just plain wrong 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.
Java Syntax (Toggle Plain Text)
while ((line = br.readLine()) != null && !"".equals(line = br.readLine().trim())) { ...... }
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.
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
![]() |
Similar Threads
- Generate Thumbnail images on the fly. (PHP)
- not your usual request for help (C++)
- how to remove a number of lines from a text file ? (Python)
- # of lines in a text file (Java)
Other Threads in the Java Forum
- Previous Thread: how can we delete a directory in java???
- Next Thread: How to pass a keyevent
Views: 9511 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for Java
911 addressbook android api append apple applet application arguments array arrays automation binary bluetooth character chat class classes client code component csv database draw eclipse error event exception file fractal ftp game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me japplet java javaarraylist javaprojects jmf jni jpanel julia linked linux list loop map method methods mobile netbeans newbie number object objects oracle oriented panel print printf problem program programming project projects recursion replaydirector reporting researchinmotion return robot rotatetext scanner screen se server set size sms socket sort sql stream string swing test threads time transfer tree ubuntu windows






