Results 1 to 3 of 3

Thread: Overwrite an existing file in java....

  1. #1
    radhika.gunti
    Guest

    Question Overwrite an existing file in java....

    Hi !!!

    I want to overwrite an existing file with the new version of the same file name.

    Right now what i am doing is like below:

    boolean success = file.renameTo(new File(destxmldir, file.getName()));

    But it does not overwrite if the same file exists before....

    I just want to overwrite it by default whether the file changed or not...

    Please help me...

    Thanks in advance.....


  2. #2
    radhika.gunti
    Guest

    Re: Overwrite an existing file in java....

    13 views...not a single reply yet...


  3. #3
    Junior Member
    Join Date
    Dec 2008
    Answers
    1

    Exclamation Re: Overwrite an existing file in java....

    Hi,

    renameTo() will not work for overwriting a file. It will return false if it could not move files from source to destination.

    You should have done this in a work around way:

    check if file exists,
    if file does notexists, create a new file/move the file in the destination location.
    Else, if file exist, write the data from the source file to the destn file using streams (either way, appending or writing newly) to the same file ..

    just a thought .. u can very well share if you have any other good logic than this.....


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact