Java Rename File renameTo(File orig, File dest)

Here you can find the source of renameTo(File orig, File dest)

Description

rename To

License

GNU General Public License

Declaration

public static boolean renameTo(File orig, File dest) 

Method Source Code

//package com.java2s;
/* This code is part of Freenet. It is distributed under the GNU General
 * Public License, version 2 (or at your option any later version). See
 * http://www.gnu.org/ for further details of the GPL. */

import java.io.File;

import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    private static Logger logger = Logger.getLogger("net.sf.thingamablog.util.io");

    public static boolean renameTo(File orig, File dest) {
        // Try an atomic rename
        // Shall we prevent symlink-race-conditions here ?
        if (orig.equals(dest))
            throw new IllegalArgumentException("Huh? the two file descriptors are the same!");
        if (!orig.exists()) {
            throw new IllegalArgumentException("Original doesn't exist!");
        }/*from  w  w  w .j  a va 2s .c o  m*/
        if (!orig.renameTo(dest)) {
            // Not supported on some systems (Windows)
            if (!dest.delete()) {
                if (dest.exists()) {
                    logger.log(Level.SEVERE, "Could not delete " + dest + " - check permissions");
                }
            }
            if (!orig.renameTo(dest)) {
                logger.log(Level.SEVERE,
                        "Could not rename " + orig + " to " + dest + (dest.exists() ? " (target exists)" : "")
                                + (orig.exists() ? " (source exists)" : "") + " - check permissions");
                return false;
            }
        }
        return true;
    }
}

Related

  1. renameSuffix(File f, String suffix)
  2. renameTempDMLScript(String dmlScriptFile)
  3. renameTo(File currentFile, String newName)
  4. renameTo(File from, File to, boolean deleteDestination)
  5. renameTo(File fromFile, File toFile)
  6. renameTo(File source, String newName)
  7. renameTo(final File srcFile, final File dstfile)
  8. renameTo(String fileName, String targetFileName)
  9. renameToBackupName(File file)