Results 1 to 9 of 9
  1. #1
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default Remove a Blank Line from Text file

    How can i Remove a Blank line from a text file.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can read the file and create a new one without the blank lines.

  3. #3
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    But My Application is like that
    I can not create new file.
    I want to remove Blank line in Existing file.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Where is the file that you want to edit and why can't you create a new file?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    To remove data from a file, the safest way is to copy the old file to a new file, leaving out the blank lines then delete or rename the old and rename the new.
    To do it in place in a single file, use a RandomAccessFile, read to the beginning of the blank line, save its location, read to the next line and copy the rest of the file over the top of the blank line.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by nitinverma View Post
    But My Application is like that
    I can not create new file.
    This restriction makes no sense.

    I want to remove Blank line in Existing file.
    And this can't be done without creating a new file unless you're talking RandomAccessFile as Norm mentions above, but you'll only want to use this if the file holds nothing but records, all the same length.

  7. #7
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

    Default

    In My Application, The length of the file vary according to the user intereaction.

  8. #8
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    If you cannot create new file, you can follow these steps :

    1. Read the whole file in a Vector of lines.
    String[] would be better

    2. Manipulate the data

    3. Create a new FileWriter, setting the append option to false.

    4. Insert the lines

    Hope that helps

  9. #9
    nitinverma is offline Member
    Join Date
    Mar 2010
    Location
    New Delhi,Vadodara
    Posts
    50
    Rep Power
    0

Similar Threads

  1. removing the last blank line from txt file
    By heartysnowy in forum New To Java
    Replies: 8
    Last Post: 12-06-2013, 01:09 PM
  2. how can we remove blank lines from a .txt
    By Camden in forum New To Java
    Replies: 12
    Last Post: 07-29-2011, 01:38 PM
  3. remove variables/line in a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-04-2009, 03:05 AM
  4. 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
  5. [SOLVED] Remove All Line from File
    By Mir in forum New To Java
    Replies: 41
    Last Post: 07-17-2008, 09: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
  •