Example usage for java.io File renameTo

List of usage examples for java.io File renameTo

Introduction

In this page you can find the example usage for java.io File renameTo.

Prototype

public boolean renameTo(File dest) 

Source Link

Document

Renames the file denoted by this abstract pathname.

Usage

From source file:Main.java

public static void moveCorruptedFileToBackup(File f) {
    if (!f.exists())
        return;/* w  ww  .ja  v a2  s. c  o  m*/
    Log.e("cr3", "Moving corrupted file " + f + " to backup.");
    File backup = getBackupFileName(f, false);
    f.renameTo(backup);
}

From source file:it.tidalwave.northernwind.core.impl.util.CachedURIResolver.java

/*******************************************************************************************************************
 *
 ******************************************************************************************************************/
private static void rename(final @Nonnull File from, final @Nonnull File to) throws IOException {
    if (!from.renameTo(to)) {
        throw new IOException("Cannot rename " + from + " to " + to);
    }/*from   w w w . j  av a2  s . c o  m*/
}

From source file:FileTools.java

/**
 * Dplace le fichier source dans le fichier rsultat
 *///from  ww  w .j  ava 2 s .  c o m
public static boolean moveFile(File source, File destination) {
    if (!destination.exists()) {
        // On essaye avec renameTo
        boolean result = source.renameTo(destination);
        if (!result) {
            // On essaye de copier
            result = true;
            result &= copyFile(source, destination);
            if (result)
                result &= source.delete();

        }
        return (result);
    } else {
        // Si le fichier destination existe, on annule ...
        return (false);
    }
}

From source file:io.treefarm.plugins.haxe.utils.HaxelibHelper.java

public static int injectPomHaxelib(String artifactId, String artifactVersion, String artifactType,
        File artifactFile, Logger logger) {
    //File unpackDirectory = getHaxelibDirectoryForArtifactAndInitialize(artifactId, artifactVersion, logger);
    File unpackDir = getLibUnpackPath(artifactId, artifactVersion);

    if (!unpackDir.exists() || artifactFile.lastModified() > unpackDir.lastModified()) {
        File libDir = getHaxelibDirectoryForArtifactAndInitialize(artifactId, artifactVersion, logger);

        if (unpackDir.exists()) {
            FileUtils.deleteQuietly(unpackDir);
        }/*from  w ww.  j  av  a2  s  . c  om*/
        if (libDir.exists()) {
            FileUtils.deleteQuietly(libDir);
        }

        UnpackHelper unpackHelper = new UnpackHelper() {
        };
        DefaultUnpackMethods unpackMethods = new DefaultUnpackMethods(logger);
        try {
            unpackHelper.unpack(unpackDir, artifactType, artifactFile, unpackMethods, null);
        } catch (Exception e) {
            logger.error(String.format("Can't unpack %s: %s", artifactId, e));
        }

        for (String fileName : unpackDir.list()) {
            if (artifactType.equals("jar")) {
                fileName = getUniqueLibPath(artifactId, artifactVersion);
            }
            File firstFile = new File(unpackDir, fileName);
            firstFile.renameTo(libDir);
            break;
        }

        unpackDir.setLastModified(artifactFile.lastModified());
    }

    int returnValue = 0;

    // set to specified version
    returnValue = setVersionFor(artifactId, artifactVersion, logger);

    return returnValue;
}

From source file:Main.java

public static File rename(File file, String newName) {
    File newFile = new File(file.getParent(), newName);
    if (!newFile.equals(file)) {
        if (newFile.exists()) {
            newFile.delete();/* ww  w. j  a v  a2  s .c  o  m*/
        }
        file.renameTo(newFile);
    }
    return newFile;
}

From source file:muffinc.yafdivj.helper.FeretHandler.java

public static void rename(String s) {
    for (File file : new File(s).listFiles()) {
        if (file.isDirectory()) {
            int size = file.listFiles(((FileFilter) new WildcardFileFilter("*.tif"))).length;

            file.renameTo(new File(file.getParent() + "/" + file.getName() + "_" + size));

        }//from  w  ww  .  java2 s.c  om
    }
}

From source file:de.flapdoodle.embed.process.io.file.Files.java

public static boolean moveFile(File source, File destination) {
    if (!source.renameTo(destination)) {
        // move konnte evtl. nicht durchgefhrt werden
        try {//w w w.j av  a2 s .  c o  m
            copyFile(source, destination);
            return source.delete();
        } catch (IOException iox) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

public static void rename(File oldFile, File newFile) {
    if (newFile.exists() && !newFile.delete()) {
        throw new RuntimeException("can not del dest file: " + newFile);
    }//w w  w. j a  va 2  s.  com
    if (!oldFile.renameTo(newFile)) {
        try {
            copy(oldFile, newFile);
            if (!oldFile.delete()) {
                // this is not so bad, but still very strange
                System.err.println("can not del source file: " + oldFile);
            }
        } catch (Exception ex) {
            throw new RuntimeException("rename failed: from: " + oldFile + " to: " + newFile, ex);
        }
    }
}

From source file:Main.java

/**
 * replaces a list of files with another list of files, indicated by names
 * @param originalList/*w ww  .  j a va2 s.c om*/
 * @param newList
 * @return
 */
public static void replaceFiles(ArrayList<String> originalList, ArrayList<String> newList)
        throws UnsupportedOperationException, OperationCanceledException {

    if (originalList.size() != newList.size())
        throw new UnsupportedOperationException();
    else {
        String name = null;
        for (int i = 0, size = originalList.size(); i < size; i++) {
            name = originalList.get(i);
            File f = new File(name);
            File newF = new File(newList.get(i));
            if (f.exists() && newF.exists()) {
                if (f.delete()) {
                    File temp = new File(name);
                    newF.renameTo(temp);
                } else {
                    throw new OperationCanceledException("Delete failed");
                }
            } else {
                throw new UnsupportedOperationException("Wrong lists of file names");
            }
        }
    }
}

From source file:Main.java

/**
 * Try to delete directory in a fast way.
 */// w ww  .  ja  va 2s .c o m
public static void deleteDirectoryQuickly(File dir) throws IOException {

    if (!dir.exists()) {
        return;
    }
    final File to = new File(dir.getAbsolutePath() + System.currentTimeMillis());
    dir.renameTo(to);
    if (!dir.exists()) {
        // rebuild
        dir.mkdirs();
    }

    // try to run "rm -r" to remove the whole directory
    if (to.exists()) {
        String deleteCmd = "rm -r " + to;
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec(deleteCmd);
            process.waitFor();
        } catch (IOException e) {

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if (!to.exists()) {
        return;
    }
    deleteDirectoryRecursively(to);
    if (to.exists()) {
        to.delete();
    }
}