Java Rename File renameExistingWithRetries(final File fromFile, final File toFile)

Here you can find the source of renameExistingWithRetries(final File fromFile, final File toFile)

Description

rename Existing With Retries

License

Open Source License

Declaration

public static void renameExistingWithRetries(final File fromFile, final File toFile) throws Exception 

Method Source Code


//package com.java2s;
/* /*from   w ww .  jav a2s.c  o  m*/
Copyright 2005-2018, Foundations of Success, Bethesda, Maryland
on behalf of the Conservation Measures Partnership ("CMP").
Material developed between 2005-2013 is jointly copyright by Beneficent Technology, Inc. ("The Benetech Initiative"), Palo Alto, California.
    
This file is part of Miradi
    
Miradi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3, 
as published by the Free Software Foundation.
    
Miradi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Miradi.  If not, see <http://www.gnu.org/licenses/>. 
*/

import java.io.File;

import java.io.IOException;

public class Main {
    public static void renameExistingWithRetries(final File fromFile, final File toFile) throws Exception {
        final int MAX_TRIES = 4;
        final int SLEEP_PER_TRY_MILLIS = 250;
        renameExistingWithRetries(fromFile, toFile, MAX_TRIES, SLEEP_PER_TRY_MILLIS);
    }

    private static void renameExistingWithRetries(final File fromFile, final File toFile, final int maxTries,
            final int sleepPerTryMillis) throws Exception {
        if (fileDoesNotExist(fromFile))
            throw new IOException("Must pass in a file that exists, file:" + fromFile.getAbsolutePath());

        for (int retryCount = 0; retryCount < maxTries; ++retryCount) {
            if (fromFile.renameTo(toFile))
                return;

            Thread.currentThread().sleep(sleepPerTryMillis);
        }

        throw new IOException(
                "Rename failed.  From:" + fromFile.getAbsolutePath() + " To:" + toFile.getAbsolutePath());
    }

    public static boolean fileDoesNotExist(final File fileToDelete) {
        return !fileToDelete.exists();
    }
}

Related

  1. renameDirectory(File directory, File newDirectory)
  2. renameDirectory(File fromDir, File toDir)
  3. renameDirectory(String in_currentDirectoryPath, String in_newDirectoryPath)
  4. renameDirectory(String newDir, String oldDir)
  5. renameExcludedFiles(File existingBinaryFolder, Collection excludedBids)
  6. renameFile(File src, File dest)
  7. renameFile(String source, String dest)
  8. renameFile(String sourcePath, String targetPath)
  9. renameFile(String src_pathname, String dest_pathname)