Results 1 to 6 of 6
  1. #1
    nanaji is offline Member
    Join Date
    May 2008
    Posts
    31
    Rep Power
    0

    Default file overwrite method

    Hi

    if i want to overwrite a file, do i need to delete the file and create file again write a bytes better or

    just

    new FileOutputStream(file) and write a new bytes to overwrite.

    Which is the best method to overwrite the file.

    Thanks
    Nanaji

  2. #2
    luckacc is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default

    do not need to delete the file
    example:
    File file=new File("c:\\t.txt");
    DataOutputStream outs = new DataOutputStream(new FileOutputStream(file,false));
    outs.write("acc".getBytes());
    outs.close();
    in FileOutputStream:
    false:overwrite the file
    true:do not overwrite,just append.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    A simple FileOutputStream(file) will also overwrite the file. You only need to use the other if you want to append.

  4. #4
    nanaji is offline Member
    Join Date
    May 2008
    Posts
    31
    Rep Power
    0

    Default

    Thank you so much luckacc.

    Thanks
    Nanaji
    Last edited by nanaji; 11-14-2008 at 07:20 AM.

  5. #5
    nanaji is offline Member
    Join Date
    May 2008
    Posts
    31
    Rep Power
    0

    Default

    By the way, a small question. By default, fos is set to False right ?

    should i declare false ?

  6. #6
    luckacc is offline Member
    Join Date
    Nov 2008
    Posts
    8
    Rep Power
    0

    Default

    default is overwrite,
    if you want to overwrite ,one argument is enough,like:new FileOutputStream(file) .or
    you can set the second argument to false.
    if you do not ,you must set the second argument to true.
    masijade said that :
    "A simple FileOutputStream(file) will also overwrite the file. You only need to use the other if you want to append.
    "

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. newbie. Reading a file, deprecated method
    By Roy Gardiner in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:08 PM
  3. Create and overwrite files
    By wmrwl in forum New To Java
    Replies: 7
    Last Post: 10-01-2008, 04:16 PM
  4. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  5. Hiding a method or function using PHP file
    By Raj Singh in forum Eclipse
    Replies: 1
    Last Post: 08-09-2007, 07:26 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
  •