Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:36:59+00:00 2026-06-17T12:36:59+00:00

I am looking for an operation to move and overwrite a File. I know

  • 0

I am looking for an operation to move and overwrite a File. I know that there is a new Method in Java7, but I was hoping to get around Java7. Also I know about the Methods in FileUtils and Guava, but the FileUtils won’t overwrite and the Guava one does not document it.

Also I am aware, I could write my own Method, well I started, but saw some Problems here and there, so I was hoping for something already done.

Do you have any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:37:00+00:00Added an answer on June 17, 2026 at 12:37 pm

    I am finished with writing my own Method, for everybody interested in a possible solution, I used the ApacheCommons FileUtils, also this is probably not perfect, but works well enough for me:

    /**
     * Will move the source File to the destination File.
     * The Method will backup the dest File, copy source to
     * dest, and then will delete the source and the backup.
     * 
     * @param source
     *            File to be moved
     * @param dest
     *            File to be overwritten (does not matter if
     *            non existent)
     * @throws IOException
     */
    public static void moveAndOverwrite(File source, File dest) throws IOException {
        // Backup the src
        File backup = CSVUtils.getNonExistingTempFile(dest);
        FileUtils.copyFile(dest, backup);
        FileUtils.copyFile(source, dest);
        if (!source.delete()) {
            throw new IOException("Failed to delete " + source.getName());
        }
        if (!backup.delete()) {
            throw new IOException("Failed to delete " + backup.getName());
        }
    }
    
    /**
     * Recursive Method to generate a FileName in the same
     * Folder as the {@code inputFile}, that is not existing
     * and ends with {@code _temp}.
     * 
     * @param inputFile
     *            The FileBase to generate a Tempfile
     * @return A non existing File
     */
    public static File getNonExistingTempFile(File inputFile) {
        File tempFile = new File(inputFile.getParentFile(), inputFile.getName() + "_temp");
        if (tempFile.exists()) {
            return CSVUtils.getNonExistingTempFile(tempFile);
        } else {
            return tempFile;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a cocoa app of mine, there is a method that does some operations
I have an operation that deals with many space delimited strings, I am looking
Looking through the perlsub and perlop manpages I've noticed that there are many references
All I'm looking for is a way to get the current drive letter that
I am looking to perform an operation with sed that appends to a specific
I am looking for a bisect operation in Haskell similar to Python's bisect_left() and
I am looking for a way that I can do bitwise operations (for crypto
I am looking to get into operating system kernel development and figured my contribution
I am looking to get into operating system kernel development and figured and have
I'm looking for the is operator, except that the type operand (right) is dynamic.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.