Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Overwriting txt file

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Overwriting txt file

    Hi again, I now have the problem that when I write something in a text file, the next time I write something in that same text file, the original text is overwritten. Here is the relevant code:
    BufferedWriter out = new BufferedWriter(new FileWriter(dir + "username.txt")); 
        			    	out.newLine();
        			    	out.write(username); 
        			    	out.close();
    So as you can see, each the the FileWriter writes something, it should first go to a new line. However, it doesn't, and instead replaces the contents of the file with the statement it's suppose to write.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: Overwriting txt file

    If you wish to append to a file, use the FileWriter constructor that accepts a boolean with true for append
    See: FileWriter

  3. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (November 28th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    18
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Overwriting txt file

    FileWriter

    The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. This constructor simply overwrite the contents in the file by the specified string but if you put the boolean value as true with the file name (argument of the constructor) then the constructor append the specified data to the file i.e. the pre-exist data in a file is not overwritten and the new data is appended after the pre-exist data.

    import java.io.*;
    class FileWrite 
    {
       public static void main(String args[])
      {
          try{
        // Create file 
        FileWriter fstream = new FileWriter("out.txt",true);
            BufferedWriter out = new BufferedWriter(fstream);
        out.write("Hello Java");
        //Close the output stream
        out.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
      }
    }

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Overwriting txt file

    Quote Originally Posted by isuru View Post
    FileWriter

    The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. This constructor simply overwrite the contents in the file by the specified string but if you put the boolean value as true with the file name (argument of the constructor) then the constructor append the specified data to the file i.e. the pre-exist data in a file is not overwritten and the new data is appended after the pre-exist data.

    import java.io.*;
    class FileWrite 
    {
       public static void main(String args[])
      {
          try{
        // Create file 
        FileWriter fstream = new FileWriter("out.txt",true);
            BufferedWriter out = new BufferedWriter(fstream);
        out.write("Hello Java");
        //Close the output stream
        out.close();
        }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }
      }
    }
    This will suit your needs exactly. Thanks for your help isuru.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Replies: 10
    Last Post: January 12th, 2011, 04:48 AM
  2. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  3. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  4. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM
  5. Problem with overwriting a file using IO package
    By marksquall in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: March 28th, 2009, 06:37 AM