Results 1 to 7 of 7
  1. #1
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default Append multiple strings to a file - Separate each with a newline?

    Hi Javapeople,

    I have a program which outputs a lot of information to the screen. I'd like to write that output to a file, but I can't quite get the hang of it. What I want is this:
    Java Code:
    Superlogfile.log ===============================
    Statement #1
    Statement #2
    Statement #3
    What I get is this:

    Java Code:
    Superlogfile.log ===============================Statement #1Statement #2Statement #3
    Obviously I want the writer to a newline at the end of each written statement; I don't know what that isn't happening. Below is my code - I've tried to make it as brief as possible.

    Any ideas? I've tried Googling for the answer, but not sure what terms to search on.

    Many thanks!
    -P



    Java Code:
    public class MainEvent {
    	
    	public static void main(String[] args) throws SecurityException, IOException {
    		
    		// Set up tools for the program
    		String filename = "Superlogfile.log";
    				
    		// Set up writer, create log file
    		Writer writer = null;
    	  	try {
    	    		writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8"));
    	    		} catch (IOException e) {
    	    			//handle the exception
    	    			//e.printStacktrace();
    	    			e.printStackTrace(System.out);
    	    		} finally {
    	    			// (I can't remember why I commented out the below command...)
    	    			//try { writer.close(); } catch(IOException silent) { }
    	    		}
    
    		// Create log file...
    	    	FileMgr myFileMgr = new FileMgr(writer, filename);
    
    		// Write something...
    		myFileMgr.AppendThisString(writer, "Statement #1\n");
    		myFileMgr.AppendThisString(writer, "Statement #2\n");
    		myFileMgr.AppendThisString(writer, "Statement #3\n");
    		
    		try { writer.close(); } catch(IOException silent) { } 
    	}
    }
    
    
    public class FileMgr {
    
      public FileMgr(Writer writer, String filename) {
          
    	try {
    		writer.write(filename + " ===============================\n");
    		} catch (IOException ex) {
    			// report
    		} finally {
    			//try {writer.close();} catch (Exception ex) {}
    		}
    	}
    	  
      void AppendThisString(Writer writer, String AddThis) {
    	try {
    		writer.append(AddThis);
    			} catch (IOException ex) {
    			// report
    			} finally {
    			//try {writer.close();} catch (Exception ex) {}
    			}
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Append multiple strings to a file - Separate each with a newline?

    From the API on BufferedWriter:

    A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system
    property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method
    to terminate each output line is therefore preferred to writing a newline character directly.

    It also depends on which application you are using to read the file. Vim processes the newline. Notepad does not.

    Regards,
    Jim
    Last edited by jim829; 02-19-2016 at 01:16 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Append multiple strings to a file - Separate each with a newline?

    The append( ... ) method is quite dull; i.e. it only appends what you pass it, it is too stupid to append a new line also ... you have to do it explicitly.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Append multiple strings to a file - Separate each with a newline?

    Java Code:
    	    		} finally {
    	    			// (I can't remember why I commented out the below command...)
    	    			//try { writer.close(); } catch(IOException silent) { }
    	    		}
    The reason you commented it out is because you stuck your file manager code outside the try block that this is associated with, so if you closed the writer here it would fail to write anything.
    Your manager code should be inside that try block, after all, if it fails to open the writer for whatever reason you don't actually want to be writing anything.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Append multiple strings to a file - Separate each with a newline?

    Quote Originally Posted by JosAH View Post
    The append( ... ) method is quite dull; i.e. it only appends what you pass it, it is too stupid to append a new line also ... you have to do it explicitly.

    The OP is doing it explicitly (even though it is not the recommended way). The newline (0xA) is in the file.
    I wonder if this is the actual code that caused the problem because as I said before, Vim displays the output
    like the OP wants.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default Re: Append multiple strings to a file - Separate each with a newline?

    LOL -- you're so right. I just tried it now and sure enough, it shutdown my log output entirely. Good catch. :)

  7. #7
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default Re: Append multiple strings to a file - Separate each with a newline?

    Quite right! thanks for this feedback, I was able to verify it this morning with trial & error. :)

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2015, 09:20 PM
  2. Replies: 2
    Last Post: 03-03-2013, 06:05 AM
  3. Replies: 2
    Last Post: 04-02-2012, 02:59 PM
  4. Problem writing multiple strings to a text file
    By Yogesh_P in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 01:58 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
  •