Results 1 to 6 of 6
  1. #1
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Question How tor write into Existing File

    Hello everybody, I have the txt file with data:
    ----------------------------------------------------------
    Line number 0: This is the text test format for javaEdu folder
    Line number 2:
    Line number 3:
    ----------------------------------------------------------
    Now I want to ADD the following: "Line number 4: testing Line 4"

    I tried it like:
    Java Code:
    import java.io.*;
    public class Buff3{
        public static void main(String [] args) throws Exception{
            String strFile="D:/javaEdu/test.txt";
            BufferedWriter bos=null;
            try{
                FileWriter fos=new FileWriter(strFile);
                bos =new BufferedWriter(fos);
                bos.write("Line 4: testing Line 4");
            }finally{
                if(bos!=null){
                    bos.flush();
                    bos.close();
                }
            }
        }
    }
    But this code does not add Line 4 but owerwrites test.txt. How can I add Line 4 into existing file without overwriting it???

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by agajantorayev View Post
    But this code does not add Line 4 but owerwrites test.txt. How can I add Line 4 into existing file without overwriting it???
    Check the other constructors for the FileWriter class.

    kind regards,

    Jos

  3. #3
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Check the other constructors for the FileWriter class.

    Jos
    Can you advise any resources to look for JavaAPI???

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,652
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by agajantorayev View Post
    Can you advise any resources to look for JavaAPI???

    Never try to program any Java without this.

    kind regards,

    Jos

  5. #5
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Great THANKS!!!!

  6. #6
    agajantorayev is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    FileWriter fos=new FileWriter(strFile);
    Should be
    FileWriter fos=new FileWriter(strFile, true);

Similar Threads

  1. how to write text on an existing image
    By jeshmal4u in forum Java 2D
    Replies: 6
    Last Post: 05-07-2010, 06:14 PM
  2. Replies: 13
    Last Post: 04-16-2010, 02:51 AM
  3. FileChooser checking for an existing file
    By sky in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2009, 11:06 PM
  4. Changing Existing Zip file using java
    By narayanan.1985 in forum Advanced Java
    Replies: 1
    Last Post: 09-30-2009, 08:27 PM
  5. Run J2ME application using existing jar file
    By chale in forum CLDC and MIDP
    Replies: 0
    Last Post: 08-25-2008, 02:11 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
  •