| | |
How can I delete empty lines from file?
![]() |
•
•
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.
•
•
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
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






