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

static File rename(File file, String newName) {
    File newFile = new File(file.getParent(), newName);
    if (!newFile.equals(file)) {
        if (newFile.exists()) {
            if (newFile.delete()) {
                Log.d("FileUtil", "Delete old " + newName + " file");
            }//from w  w w. j ava2s .com
        }
        if (file.renameTo(newFile)) {
            Log.d("FileUtil", "Rename file to " + newName);
        }
    }
    return newFile;
}

From source file:com.theelix.libreexplorer.FileManager.java

/**
 * Rename the given file/*  w  w  w  . ja  v a  2 s .  c  o  m*/
 *
 * @param sourceFile  The Target File
 * @param newFileName The New File Name
 * @throws IOException
 */
public static void rename(File sourceFile, String newFileName) throws IOException {
    sourceFile.renameTo(new File(sourceFile.getParent() + File.separator + newFileName));
}

From source file:au.org.ala.delta.util.FileUtils.java

/**
 * Checks if a file exists, if so it is backed up using an extension
 * of ".bak" and the original is deleted.
 * @param filePath the full path to the file to check and backup.
 *//*ww  w .  java 2  s .  c  o  m*/
public static void backupAndDelete(String filePath) {
    File directivesFile = new File(filePath);
    if (directivesFile.exists()) {
        File bakFile = new File(directivesFile.getAbsolutePath() + BACKUP_FILE_EXTENSION);
        directivesFile.renameTo(bakFile);

        org.apache.commons.io.FileUtils.deleteQuietly(directivesFile);
    }
}

From source file:com.ccoe.build.core.utils.FileUtils.java

public static void renameFile(File file, String ext) {
    if (!file.exists()) {
        return;/*from w  w  w  . j  a va 2  s . c  o m*/
    }
    File dest = new File(file.getParent(), file.getName() + ext);
    boolean success = file.renameTo(dest);
    if (success) {
        System.out.println("[INFO] Rename Session LOG " + dest);
    } else {
        System.out.println("[WARNING] Failed rename session LOG to " + dest);
    }
}

From source file:Main.java

public static boolean writeBmpToSDCard(Bitmap bmp, File file, int quality) {
    try {//from  w w w. j a v a  2s .  c om
        ByteArrayOutputStream baosm = new ByteArrayOutputStream();
        if (file.getPath().toLowerCase(Locale.getDefault()).endsWith(".png")) {
            bmp.compress(Bitmap.CompressFormat.PNG, quality, baosm);
        } else {
            bmp.compress(Bitmap.CompressFormat.JPEG, quality, baosm);
        }
        byte[] bts = baosm.toByteArray();

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        File tempFile = new File(file.getPath() + ".png");

        FileOutputStream fosm = new FileOutputStream(tempFile);
        BufferedOutputStream bos = new BufferedOutputStream(fosm);
        bos.write(bts);
        bos.flush();
        bos.close();
        fosm.close();

        tempFile.renameTo(file);

        return true;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}

From source file:fm.last.commons.io.LastFileUtils.java

/**
 * Moves a file "safely" - first copies it to the destination with ".part" appended to the filename, and then renames
 * it. This is useful for copying files to locations where files with certain extensions are processed. The rename
 * step should be a lot quicker than the copying step, preventing the file from being processed before it is fully
 * copied.//from w ww .j  ava2s .c  om
 * 
 * @param srcFile Source file.
 * @param destFile Destination file.
 * @throws IOException If an error occurrs moving the file.
 */
public static void moveFileSafely(File srcFile, File destFile) throws IOException {
    File partFile = new File(destFile.getParentFile(), destFile.getName() + ".part");
    FileUtils.moveFile(srcFile, partFile);
    if (!partFile.renameTo(destFile)) {
        throw new IOException(
                "Error renaming " + partFile.getAbsolutePath() + " to " + destFile.getAbsolutePath());
    }
}

From source file:com.vaadin.buildhelpers.CompileTheme.java

private static void processSassTheme(String themeFolder, String themeName, String variant, String version)
        throws Exception {

    StringBuffer cssHeader = new StringBuffer();

    version = version.replaceAll("\\.", "_");
    cssHeader.append(".v-theme-version:after {content:\"" + version + "\";}\n");
    cssHeader.append(".v-theme-version-" + version + " {display: none;}\n");

    String stylesCssDir = themeFolder + File.separator + themeName + File.separator;

    String stylesCssName = stylesCssDir + variant + ".css";

    // Process as SASS file
    String sassFile = stylesCssDir + variant + ".scss";

    ScssStylesheet scss = ScssStylesheet.get(sassFile);
    if (scss == null) {
        throw new IllegalArgumentException("SASS file: " + sassFile + " not found");
    }/*from  w w w.  j  a  va 2s .c  o m*/
    scss.compile();

    BufferedWriter out = new BufferedWriter(new FileWriter(stylesCssName));
    out.write(cssHeader.toString());
    out.write(scss.toString());
    out.close();

    System.out.println("Compiled CSS to " + stylesCssName + " (" + scss.toString().length() + " bytes)");

    createSprites(themeFolder, themeName);
    File oldCss = new File(stylesCssName);
    File newCss = new File(stylesCssDir + variant + "-sprite.css");

    if (newCss.exists()) {
        // Theme contained sprites. Renamed "styles-sprite.css" ->
        // "styles.css"
        oldCss.delete();

        boolean ok = newCss.renameTo(oldCss);
        if (!ok) {
            throw new RuntimeException("Rename " + newCss + " -> " + oldCss + " failed");
        }
    }

}

From source file:com.testmax.util.FileUtility.java

public static void renameFile(String oldFileName, String newFileName) {
    try {/*from w  ww  . ja v  a2s.  com*/
        File oldName = new File(oldFileName);
        File newName = new File(newFileName);
        if (oldName.renameTo(newName)) {
            WmLog.getCoreLogger().info("Renamed file " + oldFileName + " to " + newFileName);
        } else {
            WmLog.getCoreLogger().info("Failed to renamed file " + oldFileName + " to " + newFileName);
        }
    } catch (Exception e) {
        WmLog.getCoreLogger().info("Could not found file " + oldFileName);
    }
}

From source file:Main.java

public static boolean rename(String oldName, String newName, boolean delNewFile) {
    File oldFile = new File(oldName);
    File newFile = new File(newName);

    if (!oldFile.exists()) {
        return false;
    }/*from  ww w  .  j  av a2 s .  c  o  m*/

    boolean result = true;
    if (newFile.exists()) {
        if (delNewFile) {
            result = deleteFile(newName);
        }
    }

    if (result && oldFile.renameTo(newFile)) {
        return true;
    }
    return false;
}

From source file:Main.java

public static void moveAll(final File fromFolder, final File toFolder, final boolean overwrite,
        final boolean clearDestinationFolder) {
    if (!fromFolder.exists())
        throw new RuntimeException("From folder " + fromFolder.getAbsolutePath() + " does not exist");
    if (!toFolder.exists())
        toFolder.mkdirs();// w ww.  j  a v  a 2s.c  om
    else if (clearDestinationFolder)
        recursiveDelete(toFolder, true);
    for (File fromFile : fromFolder.listFiles()) {
        File toFile = new File(toFolder, fromFile.getName());
        if (!clearDestinationFolder && toFile.exists()) {
            if (overwrite)
                recursiveDelete(toFile);
            else
                continue;
        }
        if (!fromFile.renameTo(toFile))
            throw new RuntimeException(
                    "Cannot rename " + fromFile.getAbsolutePath() + " to " + toFile.getAbsolutePath());
    }
}