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:jetbrains.exodus.io.FileDataReader.java

private static boolean renameFile(@NotNull final File file) {
    final String name = file.getName();
    return file.renameTo(new File(file.getParent(),
            name.substring(0, name.indexOf(LogUtil.LOG_FILE_EXTENSION)) + DELETED_FILE_EXTENSION));
}

From source file:edu.unc.lib.dl.util.FileUtils.java

/**
 * Moves a file or directory. Uses rename operation if possible. This operation may not be atomic.
 *
 * @param src/*from w  ww  .  j  a  v  a2 s  .  c  om*/
 * @param dest
 * @return
 * @throws IOException
 */
public static void renameOrMoveTo(File src, File dest) throws IOException {
    if (!src.renameTo(dest)) {
        // cannot rename, try copy and delete
        log.warn("Cannot rename directory " + src + " to queue location " + dest
                + ", forced to perform slower copy and delete operations.");
        FileUtils.copyFolder(src, dest);
        if (!src.delete()) {
            log.warn("Cannot delete original file in move operation.");
        }
    }
}

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()) {
            if (newFile.delete()) {
            }/*from w w w  .  j  av  a2 s.  c o m*/
        }
        if (file.renameTo(newFile)) {
        }
    }
    return newFile;
}

From source file:mobac.program.EnvironmentSetup.java

public static void upgrade() {
    FileFilter ff = new NamePatternFileFilter("tac-profile-.*.xml");
    File profilesDir = DirectoryManager.currentDir;
    File[] files = profilesDir.listFiles(ff);
    for (File f : files) {
        File dest = new File(profilesDir, f.getName().replaceFirst("tac-", "mobac-"));
        f.renameTo(dest);
    }//w  w  w . j  a v a 2  s  .  c  o m
}

From source file:com.appeligo.lucene.DidYouMeanIndexer.java

public static void createDefaultSpellIndex(String indexDir, String spellDir) throws IOException {

    String newSpellDir = spellDir + ".new";
    File newSpellDirFile = new File(newSpellDir);
    if (newSpellDirFile.exists()) {
        String[] dirFiles = newSpellDirFile.list();
        for (String dirFile : dirFiles) {
            File f = new File(newSpellDirFile, dirFile);
            if (!f.delete()) {
                throw new IOException("Could not delete " + f.getAbsolutePath());
            }/*from w w w. j av  a  2  s.c om*/
        }
        if (!newSpellDirFile.delete()) {
            throw new IOException("Could not delete " + newSpellDirFile.getAbsolutePath());
        }
    }

    /* This was for the original programIndex, but we found out that stemming was bad, and you get better
     * spelling suggestions if you can specify a single field, so we combined them.
    for (String field : new String[]{"text","description","programTitle", "episodeTitle", "credits", "genre"}) {
      createSpellIndex(field, FSDirectory.getDirectory(indexDir), FSDirectory.getDirectory(newSpellDir));
    }
    */

    createSpellIndex("compositeField", FSDirectory.getDirectory(indexDir),
            FSDirectory.getDirectory(newSpellDir));

    String oldSpellDir = spellDir + ".old";
    File oldSpellDirFile = new File(oldSpellDir);
    if (oldSpellDirFile.exists()) {
        String[] dirFiles = oldSpellDirFile.list();
        for (String dirFile : dirFiles) {
            File f = new File(oldSpellDirFile, dirFile);
            if (!f.delete()) {
                throw new IOException("Could not delete " + f.getAbsolutePath());
            }
        }
        if (!oldSpellDirFile.delete()) {
            throw new IOException("Could not delete " + oldSpellDirFile.getAbsolutePath());
        }
    }

    File spellDirFile = new File(spellDir);
    if (spellDirFile.exists() && !spellDirFile.renameTo(oldSpellDirFile)) {
        throw new IOException("could not rename " + spellDirFile.getAbsolutePath() + " to "
                + oldSpellDirFile.getAbsolutePath());
    }
    /* there is some small risk here that someone might try to get the spell index when the file isn't there yet */
    /* I don't know of any way to really synchronize that from this class, and the risk is minor, unlikely, and not catastrophic */
    spellDirFile = new File(spellDir);
    if (!newSpellDirFile.renameTo(spellDirFile)) {
        // What really bugs me is you can't close a SpellChecker, and I think that prevents us from renaming
        // the spell index directory (at least on Windows Vista), so let's copy the files instead
        /*
        throw new IOException("could not rename "+newSpellDirFile.getAbsolutePath()+" to "+spellDirFile.getAbsolutePath());
        */
        if (!spellDirFile.mkdir()) {
            throw new IOException("Couldn't make directory " + spellDirFile.getAbsolutePath());
        }
        String[] dirFiles = newSpellDirFile.list();
        for (String dirFile : dirFiles) {
            File f = new File(newSpellDirFile, dirFile);
            File toF = new File(spellDirFile, dirFile);
            InputStream is = new BufferedInputStream(new FileInputStream(f.getAbsolutePath()));
            OutputStream os = new BufferedOutputStream(new FileOutputStream(toF.getAbsolutePath()));
            int b;
            while ((b = is.read()) != -1) {
                os.write(b);
            }
            is.close();
            os.close();
            /* I'd like to do this, but the same reason the rename won't work is why this
             * won't work... this current program still has one or more of the files open.
            if (!f.delete()) {
               throw new IOException("Could not delete "+f.getAbsolutePath());
            }
            }
            if (!newSpellDirFile.delete()) {
            throw new IOException("Could not delete "+newSpellDirFile.getAbsolutePath());
            }
             */ }
    }
}

From source file:Main.java

public static boolean rename(String srcPath, String dstPath) {
    File srcFile = null;
    File dstFile = null;//  w w w  .  j a v a 2  s .c o m
    if (srcPath == null || dstPath == null) {
        return false;
    }
    srcFile = new File(srcPath);
    if (!srcFile.exists()) {
        return false;
    }
    dstFile = new File(dstPath);
    srcFile.renameTo(dstFile);
    return true;
}

From source file:Main.java

public static void reNameSuffix(File dir, String oldSuffix, String newSuffix) {
    if (dir.isDirectory()) {
        File[] listFiles = dir.listFiles();
        for (int i = 0; i < listFiles.length; i++) {
            reNameSuffix(listFiles[i], oldSuffix, newSuffix);
        }/*from   ww  w . jav a2 s  .  c  om*/
    } else {
        dir.renameTo(new File(dir.getPath().replace(oldSuffix, newSuffix)));
    }
}

From source file:Main.java

public static void copyAsset(Context context, AssetManager am, boolean force) {
    File newDir = getSDDir(context);
    File sd = Environment.getExternalStorageDirectory();
    if (sd != null && sd.exists() && sd.isDirectory() && newDir.exists() && newDir.isDirectory()) {
        File hpDir = new File(sd, ".hp48");
        if (hpDir.exists()) {
            File allFiles[] = hpDir.listFiles();
            if (allFiles != null && allFiles.length > 0) {
                Log.i("x48",
                        "Moving x48 files from the old dir " + sd.getAbsolutePath() + " to the proper one :");
                for (File file : allFiles) {
                    File newFile = new File(newDir, file.getName());
                    Log.i("x48", "Moving " + file.getAbsolutePath() + " to " + newFile);
                    file.renameTo(newFile);
                }/*w w w . j av  a  2s.co  m*/
            }
            Log.i("x48", "Deleting old directory");
            hpDir.delete();
        }
    }
    copyAsset(am, newDir, force);
}

From source file:Main.java

public static boolean copyAsset(Context context, AssetManager am, boolean force) {
    File newDir = getSDDir(context);
    File sd = Environment.getExternalStorageDirectory();
    if (sd != null && sd.exists() && sd.isDirectory() && newDir != null && newDir.exists()
            && newDir.isDirectory()) {
        File hpDir = new File(sd, ".hp48");
        if (hpDir.exists()) {
            File allFiles[] = hpDir.listFiles();
            if (allFiles != null && allFiles.length > 0) {
                Log.i("x48",
                        "Moving x48 files from the old dir " + sd.getAbsolutePath() + " to the proper one :");
                for (File file : allFiles) {
                    File newFile = new File(newDir, file.getName());
                    Log.i("x48", "Moving " + file.getAbsolutePath() + " to " + newFile);
                    file.renameTo(newFile);
                }/*from   ww  w.j a va  2 s  .  c o m*/
            }
            Log.i("x48", "Deleting old directory");
            hpDir.delete();
        }
    }
    return copyAsset(am, newDir, force);
}

From source file:org.openmrs.module.report.util.FileUtils.java

public static void renameFile(File file, String newName) {
    file.renameTo(new File(newName));
}