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:
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();
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?
Thanks in advance