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 renameImageFileName(Context context, String oldImgName, String newImgName) {
    String dir = getDir(context, oldImgName);
    File oldFile = new File(dir);
    dir = getDir(context, newImgName);//from  w w w .ja  v  a2 s .c o  m
    File newFile = new File(dir);
    oldFile.renameTo(newFile);
}

From source file:Main.java

public static boolean renameDict(final File dictFile, final File newDictFile) {
    if (dictFile.isFile()) {
        return dictFile.renameTo(newDictFile);
    } else if (dictFile.isDirectory()) {
        final String dictName = dictFile.getName();
        final String newDictName = newDictFile.getName();
        if (newDictFile.exists()) {
            return false;
        }/*from  w  ww.java2  s.  c om*/
        for (final File file : dictFile.listFiles()) {
            if (!file.isFile()) {
                continue;
            }
            final String fileName = file.getName();
            final String newFileName = fileName.replaceFirst(Pattern.quote(dictName),
                    Matcher.quoteReplacement(newDictName));
            if (!file.renameTo(new File(dictFile, newFileName))) {
                return false;
            }
        }
        return dictFile.renameTo(newDictFile);
    }
    return false;
}

From source file:Main.java

public static boolean renameFile(String src, String dst) {
    File srcFile = new File(src);
    File dstFile = new File(dst);
    if (dstFile.exists()) {
        dstFile.delete();//w w w  . ja va  2  s .  c o  m
    }
    return srcFile.renameTo(dstFile);
}

From source file:Main.java

public static boolean restoreFromBackup(File f) {
    File backup = new File(f.getAbsolutePath() + ".good.bak.2");
    if (f.exists())
        f.delete();/*from  www  .  ja v  a  2 s. c o  m*/
    if (backup.exists()) {
        if (backup.renameTo(f))
            return true;
    }
    return false;
}

From source file:Main.java

public static void rename(String oldPath, String newPath) {
    File oldFile = new File(oldPath);
    File newFile = new File(newPath);
    if (oldFile.exists())
        oldFile.renameTo(newFile);
    //        if (!KCNativeUtil.renameExt(oldPath, newPath)) {
    //            File oldFile = new File(oldPath);
    //            try {
    //                InputStream is = new FileInputStream(oldFile);
    //                OutputStream os = new FileOutputStream(oldPath);
    //                byte[] buffer = getThreadSafeByteBuffer();
    ////from ww w  .  j a v  a  2s. c  o  m
    //                int lenRead;
    //                while ((lenRead = is.read(buffer)) != -1) {
    //                    os.write(buffer, 0, lenRead);
    //                }
    //
    //                is.close();
    //                os.close();
    //
    //                if (oldFile.exists())
    //                    oldFile.delete();
    //            } catch (Exception e) {
    //                e.printStackTrace();
    //            } finally {
    //            }
    //        }
}

From source file:com.alibaba.zonda.logger.server.util.FileUtil.java

public static boolean rename(File f1, File f2) {
    ensurePathExists(f2.getParent());/*from   ww  w .ja va 2s. c o  m*/
    if (f2.exists())
        f2.delete();
    return f1.renameTo(f2);
}

From source file:Main.java

private static File getBackupFileName(File f, boolean isGoodBackup) {
    File f2 = null;/*w w  w .  jav  a  2  s. c  o  m*/
    String prefix = f.getAbsolutePath() + (isGoodBackup ? ".good.bak." : ".corrupted.bak.");
    for (int i = MAX_BACKUP_FILES - 1; i > 2; i--) {
        File to = new File(prefix + i);
        File from = new File(prefix + (i - 1));
        if (to.exists())
            to.delete();
        if (from.exists()) {
            if (!from.renameTo(to))
                Log.e("cr3", "Cannot rename DB file " + from + " to " + to);
        }
    }
    f2 = new File(prefix + 2);
    if (f2.exists())
        if (!f2.delete())
            Log.e("cr3", "Cannot remove DB file " + f2);
    return f2;
}

From source file:org.powertac.common.OrderbookTests.java

@AfterClass
public static void saveLogs() throws Exception {
    File state = new File("log/test.state");
    state.renameTo(new File("log/OrderbookTests.state"));
    File trace = new File("log/test.trace");
    trace.renameTo(new File("log/OrderbookTests.trace"));
}

From source file:Main.java

private static boolean deleteFileSafely(File file) {
    if (file != null) {
        String tmpPath = file.getAbsolutePath() + System.currentTimeMillis();
        File tmp = new File(tmpPath);
        boolean result = file.renameTo(tmp);
        return result && tmp.delete();
    }//from www  . j a  v  a2  s.co m
    return false;
}

From source file:Main.java

private static File commit(String basename, File tmpFile) throws IOException {
    try {/*from   w  w  w  . j  ava2  s  . c  o m*/
        final String backupName = basename + "~";
        final File desired = new File(basename);
        final File backup = new File(backupName);
        backup.delete();
        if (desired.exists()) {
            if (!desired.renameTo(new File(backupName)))
                throw new IOException("can't rename to " + backupName);
        }
        if (!tmpFile.renameTo(new File(basename)))
            throw new IOException("can't rename to " + basename);
    } catch (IOException x) {
        tmpFile.delete();
        throw x;
    }
    return new File(basename);
}