public static void fileRename(final File file, final String newPath, final Class clazz) {
        File newDestFile = new File(newPath);
        File lockFile = new File(newDestFile.getAbsolutePath() + ".lock");
        try {
            synchronized (clazz) {
                FileChannel channel = new RandomAccessFile(lockFile, "rw").getChannel();
                FileLock lock = channel.lock();
                try {
                    try {
                        // Create parent directory structure if necessary
                        FileUtils.mkParentDirs(newDestFile);
                        Files.move(file.toPath(), newDestFile.toPath(),
                                StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException ioe) {
                        throw new CoreException("Unable to move file " + file +
                                " to destination " + newDestFile + ": " + ioe.getMessage());
                    }
                } finally {
                    lock.release();
                    channel.close();
                }
            }
        } catch (IOException e) {
            System.err.println("IOException: " + e.getMessage());
            e.printStackTrace(System.err);
            throw new CoreException("Unable to rename file: " + e.getMessage(), e);
        }
    }
public static void fileRename ( final File file , final String newPath , final Class clazz ) { File newDestFile = new File ( newPath ) ; File lockFile = new File ( newDestFile . getAbsolutePath ( ) + ".lock" ) ; try { synchronized ( clazz ) { FileChannel channel = new RandomAccessFile ( lockFile , "rw" ) . getChannel ( ) ; FileLock lock = channel . lock ( ) ; try { try { FileUtils . mkParentDirs ( newDestFile ) ; Files . move ( file . toPath ( ) , newDestFile . toPath ( ) , StandardCopyOption . REPLACE_EXISTING ) ; } catch ( IOException ioe ) { throw new CoreException ( "Unable to move file " + file + " to destination " + newDestFile + ": " + ioe . getMessage ( ) ) ; } } finally { lock . release ( ) ; channel . close ( ) ; } } } catch ( IOException e ) { System . err . println ( "IOException: " + e . getMessage ( ) ) ; e . printStackTrace ( System . err ) ; throw new CoreException ( "Unable to rename file: " + e . getMessage ( ) , e ) ; } }
Rename a file.
Uses Java's nio library to use a lock.
@param file  File to rename
@param newPath  Path for new file name
@param clazz Class associated with lock
@throws com.dtolabs.rundeck.core.CoreException A CoreException is raised if any underlying I/O
operation fails.