Results 1 to 9 of 9
  1. #1
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default 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:
    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();
    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
    Last edited by heartysnowy; 11-04-2009 at 06:50 PM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Well don't write out the newline every time.

  3. #3
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    i still want to maintain the same format, which is why i used newLine()

    if no newLine, the b.txt will be like 'bb1b2'.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    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).

  5. #5
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    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.

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

    Default

    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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Throw your compiler away because your code shouldn't compile: you are comparing a String with a character which isn't valid Java.

    w.r.t your problem: don't write a line when it doesn't start with an 'a' or a 'b'. There will be no blank lines in your output then.

    kind regards,

    Jos

  8. #8
    vpkathirvel is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default Re: removing the last blank line from txt file

    Quote Originally Posted by heartysnowy View Post
    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:
    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();
    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
    if not a first line add newLine(); above to the write(aString);
    like
    bwStream2.newLine();
    bwStream2.write(aString);

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: removing the last blank line from txt file

    This thread has been dead for a long time; it doesn't even smell funny anymore; I'm closing it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  2. Write to line in file
    By blackstormattack in forum New To Java
    Replies: 3
    Last Post: 03-09-2009, 12:59 PM
  3. Replies: 7
    Last Post: 11-19-2008, 02:09 PM
  4. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 12:31 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 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
  •