• Post Reply Bookmark Topic Watch Topic
  • New Topic

remove blank line from text file

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I remove blank line in text file?
My code:

I want to implement this code:

But the problem that I am facing is this line (because of int temp not String):

Is there any way to solve this problem?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tai Yo wrote:Is there any way to solve this problem?

You already have it!

Use a buffered reader to read lines instead of reading bytes.
Also you can use the more convenient (IMO) if(line.trim().isEmpty())
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should read sistream and write to fostream.
That is why I have to read bytes.

I wrote like this:

But it gives me error because of int temp.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the whole point. Since you want to identify empty lines, you need to read lines and not bytes.
Use BufferedReader and BufferedWriter
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely you would use isEmpty()?
if (line.isEmpty() || line.trim().isEmpty()) ...
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my question was not clear.
I want to remove blank line from "result.txt" file which is the combination of SequenceInputStream.
Can someone show me sample how to remove empty line in case of SequenceInputStream, i.e. int temp.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Surely you would use isEmpty()?
if (line.isEmpty() || line.trim().isEmpty()) ...

I have found (classic Doh! moment) that isEmpty() considers white space to be a legal character. Thats why always trim before using isEmpty()
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tai Yo wrote:I think my question was not clear.
I want to remove blank line from "result.txt" file which is the combination of SequenceInputStream.
Can someone show me sample how to remove empty line in case of SequenceInputStream, i.e. int temp.


I will repeat what I mentioned before. You need to read lines instead of reading bytes else how will you determine if a line is empty?
You can use a BufferedReader on conjunction with SequenceInputStream
Check out the example at http://www.java2s.com/Tutorial/Java/0140__Collections/TheSequenceInputStreamClass.htm
 
Tai Yo
Ranch Hand
Posts: 98
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Maneesh Godbole Thank you so much for link. I got it. I am very grateful for your help!!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome!
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Use a buffered reader to read lines instead of reading bytes.

And if you're on version 8, it seems to me that something like this might work (Moderators, please correct if my syntax is wrong ).
Winston
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:. . . isEmpty() considers white space to be a legal character. Thats why always trim before using isEmpty()
That is normal. A String containing a space is not empty. If you want to eliminate lines containing whitespace only, you need trim, so you need trim() too. You will get faster performance with a short‑circuit operator however.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . on version 8, it seems to me that something like this might work . . .
Yes, it would. You might not want to print the individual lines. You can consider collecting all the lines into a List and writing the entire contents of that List to a new file.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything about GUIs in this thread so I'm moving it out of the Swing/AWT/SWT forum.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:. . . isEmpty() considers white space to be a legal character. Thats why always trim before using isEmpty()
I forgot to say yesterday that trim() has a rather strange definition of whitespace. It does say it returns an empty String if you call it on all whitespace (by its definition).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!