How can i Remove a Blank line from a text file.
Printable View
How can i Remove a Blank line from a text file.
You can read the file and create a new one without the blank lines.
But My Application is like that
I can not create new file.
I want to remove Blank line in Existing file.
Where is the file that you want to edit and why can't you create a new file?
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.
This restriction makes no sense.
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.Quote:
I want to remove Blank line in Existing file.
In My Application, The length of the file vary according to the user intereaction.
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
Thank u, I will try these Steps.