Search code examples
javaio

Is there an operation to move and overwrite files?


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?


Solution

  • 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;
        }
    }