Results 1 to 7 of 7
- 11-04-2009, 06:44 PM #1
removing the last blank line from txt file
Hello,
this is my sample source text file
source
a
b
b1
a1
b2
my program read the source file and create/write another 2 files (e.g a.txt and b.txt)
a.txt stores anything related 'a' and the same logic goes for b.txt.
my problem is after generating either a.txt or b.txt files, I had a blank line at the end of the file.
(e.g Sample
b.txt
b
b1
b2
"\n" <--- here
How shall I remove that blank line?
here is my code:
I knew its because of this line (bwStream2.newLine()) as it adds a new line after writing the string...so i tried to add in the validator before creating a new line but it doesn't solve fully. Is there a way to eliminate that blank line?Java Code:FileReader frStream = new FileReader( "alpha.txt" ); BufferedReader brStream = new BufferedReader( frStream ); FileWriter fwStream = new FileWriter( "a.txt" ); BufferedWriter bwStream = new BufferedWriter( fwStream ); FileWriter fwStream2 = new FileWriter("b.txt"); BufferedWriter bwStream2 = new BufferedWriter (fwStream2); String aString = brStream.readLine(); while ( aString != null ) { if ( aString=='a'){ bwStream.write(aString); bwStream.newLine(); } else if(aString == 'b' ) { bwStream2.write(aString); bwStream2.newLine(); } aString = brStream.readLine(); } brStream.close(); bwStream.close(); bwStream2.close();
Thanks in advanceLast edited by heartysnowy; 11-04-2009 at 06:50 PM.
- 11-04-2009, 06:50 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Well don't write out the newline every time.
- 11-04-2009, 06:56 PM #3
i still want to maintain the same format, which is why i used newLine()
if no newLine, the b.txt will be like 'bb1b2'.
- 11-04-2009, 07:01 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Think about it. You can't automatically add it after every line because you don't know if you are going to be adding any more lines to the file. So you want to add the newline before you you write out the rest of the data (except for the first line of course).
- 11-04-2009, 07:10 PM #5
As I had stated on my first post, I tried adding in 'counter' validator before I write the string, but it doesn't fully work...only work for 1 output text file (as my logic is not completely accurate)... I could solve it using 2 counters but what I hv been trying to achieve is a way to eliminate the last blank line from test file....
do I hv to re-read the generated text file (in this case a.txt or b.txt),look for the blank, remove it and re-write the new file?Last edited by heartysnowy; 11-04-2009 at 07:18 PM.
-
I don't think you need a counter, just two boolean variables, say firstLineA and firstLineB, set them both to true. When you need to write to file A, check firstLineA and if false, output a new line to the file before writing the line you want to output. If true, don't output the new line first and then set it to false. You should be able to figure this one out.
- 11-04-2009, 07:15 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Need to read an .ini and .abook file line by line (both files contain texts)
By ollyworks in forum Java AppletsReplies: 4Last Post: 09-10-2009, 10:18 AM -
Write to line in file
By blackstormattack in forum New To JavaReplies: 3Last Post: 03-09-2009, 12:59 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-24-2008, 12:31 AM -
Reading in data from file line by line
By bluekswing in forum New To JavaReplies: 1Last Post: 10-02-2007, 12:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks