• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

remove blank line from text file

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic