Results 1 to 5 of 5

Thread: IOStreams

  1. #1
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Question IOStreams

    Hi all,
    please see below code...
    Java Code:
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    public class ChracterStreams
    {
      public static void main(String[] args) throws IOException
      {
          FileReader in=null;
          FileWriter out=null;
          try
          {
              in=new FileReader("E:/Documents and Settings/Naresh/Desktop/java/abc.txt");
              out=new FileWriter("E:/Documents and Settings/Naresh/Desktop/java/xyz.txt"); 
              int c;
              while((c=in.read())!=-1)
                  out.write(c);
          }
          finally
          {
              if(in!=null)
                  in.close();
              if(out!=null)
                  out.close();
          }
      }        
    
    }
    It successfully writes data from abc.txt to xyz.txt.
    but it overwrites data in xyz.txt.
    I just want to append new data in abc.txt to the data in xyz.txt.
    now what i have to do ?
    anyone please tell me....
    Thankq very much.

  2. #2
    XiaoXiao is offline Member
    Join Date
    Jan 2008
    Posts
    6
    Rep Power
    0

    Default Just use different constructor

    FileWriter has an other constructor where you can specify whether to append or overwrite.
    use

    out=new FileWriter("E:/Documents and Settings/Naresh/Desktop/java/xyz.txt",true);
    the boolean true will set that to append mode.

  3. #3
    sireesha is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    thankq very much

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Ahh thanks for that information XiaoXiao. I never knew that. That will come in handy!!! =]

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

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