Results 1 to 11 of 11
  1. #1
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default [SOLVED] remove all white space from text file

    Hi all

    I'm new to this forum and pretty new to java in general.

    I've got a question. Does anyone know how to remove all white space from a text file and i don't mean duplicate white space, i mean actually remove all spaces so there are none at all?

    I hope that makes sense

    Thanks

    loki

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    what exactly do you need to know for this task? how to read/write a file? how to remove whitespace from a string? i'm sure you can google both and get answers quickly.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    If you read in a line from a text file you use the trim() method to remove leading and trailing white space.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Check out RandomAccessFile... There is a setLength() method...
    Who Cares... As Long As It Works...

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You can do it in two ways. But the second way is not encourage to use.

    Case 1:
    Using regular expressions. Leading and trailing white spaces can be deleted using trim() method of the String class. To remove spaces in between you can use a regular expression.

    Case 2:
    Using StringTokernizer. This is straight forward. Tokernize the string based on space character. Then append each element in to the previous element and build the final string object.

  6. #6
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    If I recall correctly, you can use the replaceAll method in the String class.
    i.e myString.replaceAll(" ", "");

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    it might be more efficient to use a whitespace character in multiples "\\s+" or at least i think that is the right regex (never been good at it)

  8. #8
    logicbug's Avatar
    logicbug is offline Member
    Join Date
    Jan 2009
    Location
    The Great White North, eh?
    Posts
    76
    Rep Power
    0

    Default

    yeah, that might work better, I've always found regular expressions to be highly irregular and genrally painful in the frontal lobe area. lol

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Actually regular expressions are more powerful, and easy to use, if you know how to use it correctly. And also, it's not easy to learn about regular expressions in a short period of time. But spending more time is not wasting at all.

  10. #10
    loki is offline Member
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    Hey guys thanks for the replies it's much appreciated!

    I googled regular expressions like some of you said and found some useful info.

    I tried to post the link to a website that i found, but the forum wouldn't let me as i've posted less than 20 posts.

    Thanks again for the help

    loki

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. remove variables/line in a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-04-2009, 03:05 AM
  2. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 03:54 PM
  3. Remove duplicate lines from a text file
    By Dirt.Diver in forum New To Java
    Replies: 15
    Last Post: 06-25-2008, 02:08 PM
  4. White Space Issue
    By sibythoma1984 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-22-2008, 12:05 PM
  5. Waste Space & Text Field
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 2
    Last Post: 12-01-2007, 07:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •