Results 1 to 4 of 4
  1. #1
    Yourleader15 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Appending to a file to write to

    Hi everyone, I am new to input/output streaming, and I just want to know how to append to a file.

    Here is my code:

    Java Code:
    s = "this is my text"
    
    public static void main(String[] args) throws IOException {
    		         
    	PrintWriter outputStream = null;
    
    		         try {
    		           
    		             outputStream = new PrintWriter(new FileWriter("output.txt"));
    
    		                 outputStream.println(s);
    		         } finally {
    
    		             if (outputStream != null) {
    		                 outputStream.close();
    		             }
    		    }
    		}

    It does write to the output, but when I change the value of s and rerun the program, it just erases what was there before and writes on top of it. Is there a separate command for appending to a file and will it start on a new line?

    also, how would I indicate the path, right now it just puts it in my project folder.

    Thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    The FileWriter class has more than one constructor!

  3. #3
    Yourleader15 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default

    Alright, that was perfect and solved my one problem.

    Now I'm just curious how to change the path of output.txt

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    The FileWriter class has more than one constructor!

    What I'm alluding to is that you can use the constructor that takes a File object. Create the File object with the required path.

Similar Threads

  1. problem with appending to file
    By Ninjateckie in forum Java Applets
    Replies: 0
    Last Post: 03-14-2011, 09:32 PM
  2. Appending WAV file at the end of an existing file
    By phil128 in forum New To Java
    Replies: 2
    Last Post: 12-12-2010, 12:20 AM
  3. Replies: 6
    Last Post: 12-01-2010, 06:31 AM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Appending to a File Used for Serialization
    By Lil_Aziz1 in forum Advanced Java
    Replies: 8
    Last Post: 02-02-2010, 11:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •