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:org.lol.reddit.common.General.java

public static void moveFile(final File src, final File dst) throws IOException {

    if (!src.renameTo(dst)) {

        copyFile(src, dst);/*from   ww  w .j a  va  2s. com*/

        if (!src.delete()) {
            src.deleteOnExit();
        }
    }
}

From source file:com.opensymphony.util.FileUtils.java

public final static void writeAndBackup(File file, String content) {
    try {//from ww w.  ja  va 2s . co  m
        DateFormat backupDF = new SimpleDateFormat("ddMMyy_hhmmss");

        File backupDirectory = checkBackupDirectory(file);

        File original = new File(file.getAbsolutePath());

        File backup = new File(backupDirectory, original.getName() + "." + backupDF.format(new Date()));

        if (log.isDebugEnabled()) {
            log.debug("Backup file is " + backup);
        }

        original.renameTo(backup);

        BufferedWriter out = new BufferedWriter(new FileWriter(file));

        out.write(content);
        out.close();
    } catch (FileNotFoundException e) {
        log.warn("File not found", e);
    } catch (IOException e) {
        log.error(e);
    }
}

From source file:com.knowbout.epg.EPG.java

private static void updateSitemap(String sitemap) {
    int lastslash = sitemap.lastIndexOf('/');
    String inprogress = sitemap.substring(0, lastslash) + "/inprogress-" + sitemap.substring(lastslash + 1);
    String marker = "<!-- EVERYTHING BELOW IS AUTOMATICALLY GENERATED -->";
    String baseurl = null;//from  w w w  .  j  a va 2 s  .c om
    try {
        PrintStream doc = new PrintStream(new GZIPOutputStream(new FileOutputStream(inprogress)));
        BufferedReader orig = new BufferedReader(
                new InputStreamReader(new GZIPInputStream(new FileInputStream(sitemap))));
        String line = orig.readLine();
        while (line != null) {
            if ((line.indexOf("</urlset>") >= 0) || (line.indexOf(marker) >= 0)) {
                break;
            }
            if (baseurl == null) {
                if (line.indexOf("<loc>") >= 0) {
                    int http = line.indexOf("http://");
                    int nextslash = line.indexOf("/", http + 7);
                    baseurl = line.substring(http, nextslash);
                }
            }
            doc.println(line);
            line = orig.readLine();
        }
        doc.println(marker);
        Set<String> teams = new HashSet<String>();
        HibernateUtil.openSession();
        try {
            ScrollableResults scroll = Program.selectAllTeams();
            while (scroll.next()) {
                Program program = (Program) scroll.get(0);
                addToSitemap(doc, baseurl, program);
                teams.add(program.getSportName() + ":" + program.getTeamName());
            }
            scroll = Program.selectAllShowsMoviesSports();
            while (scroll.next()) {
                Program program = (Program) scroll.get(0);
                if (program.isSports()) {
                    if (!teams.contains(program.getSportName() + ":" + program.getHomeTeamName())) {
                        Program home = Program.selectByTeam(program.getSportName(), program.getHomeTeamName());
                        addToSitemap(doc, baseurl, home);
                        teams.add(home.getSportName() + ":" + home.getTeamName());
                    }
                    if (!teams.contains(program.getSportName() + ":" + program.getAwayTeamName())) {
                        Program away = Program.selectByTeam(program.getSportName(), program.getAwayTeamName());
                        addToSitemap(doc, baseurl, away);
                        teams.add(away.getSportName() + ":" + away.getTeamName());
                    }
                } else {
                    addToSitemap(doc, baseurl, program);
                }
            }
        } finally {
            HibernateUtil.closeSession();
        }
        doc.println("</urlset>");
        doc.close();
        orig.close();
        File origFile = new File(sitemap);
        File backupFile = new File(sitemap + ".bak");
        File inprogressFile = new File(inprogress);
        backupFile.delete();
        if (!origFile.renameTo(backupFile)) {
            throw new IOException("Could not rename " + origFile + " to " + backupFile);
        }
        if (!inprogressFile.renameTo(origFile)) {
            throw new IOException("Could not rename " + inprogressFile + " to " + origFile);
        }
    } catch (FileNotFoundException e) {
        log.error("Could not write to " + inprogress, e);
    } catch (IOException e) {
        log.error("IO Exception for " + inprogress + " or " + sitemap, e);
    }
}

From source file:com.taobao.android.builder.tools.BuildHelper.java

public static File reSign(File apkFile, DefaultSigningConfig signConfig) throws IOException, SigningException {

    String filePath = apkFile.getAbsolutePath();
    File workDir = new File(apkFile.getParentFile(), "_tmpsign");
    apkFile.renameTo(new File(workDir, apkFile.getName()));

    File unsignedApk = new File(apkFile.getParentFile(), apkFile.getName() + "_");
    Pattern pattern = Pattern.compile("META-INF");
    ZipUtils.removeZipEntry(apkFile, pattern, unsignedApk);

    AtlasBuildContext.sBuilderAdapter.androidSigner.signFile(unsignedApk, apkFile, signConfig);

    apkFile.renameTo(new File(filePath));
    FileUtils.deleteDirectory(workDir);/*ww w . ja v a 2 s  . c  o  m*/
    return apkFile;
}

From source file:com.edgenius.core.util.FileUtil.java

/**
 * @param oldFileNodeDir/*from   w  w  w . ja  v  a  2 s.  c  om*/
 * @param newFileNodeDir
 * @return 
 */
public static boolean rename(String oldFileNodeDir, String newFileNodeDir) {
    File oldF = new File(oldFileNodeDir);
    File newF = new File(newFileNodeDir);
    return oldF.renameTo(newF);

}

From source file:com.hazelcast.simulator.utils.FileUtils.java

public static void rename(File source, File target) {
    if (!source.exists()) {
        return;/*from  ww w.  j av  a  2s  .c  o  m*/
    }
    if (!source.renameTo(target)) {
        throw new FileUtilsException(
                format("Could not rename [%s] to [%s]", source.getAbsolutePath(), target.getAbsolutePath()));
    }
}

From source file:com.taobao.android.builder.tools.zip.BetterZip.java

public static boolean addFile(File zipFile, String path, File file) throws IOException {

    if (file.isDirectory()) {
        return false;
    }/*from w  w  w  .  j  a v a  2s.co m*/

    File rootDir = new File(file.getParentFile(), "_tmp");
    rootDir.delete();

    FileUtils.copyFile(file, new File(rootDir, path));

    //zip -r taobao-android-debug.apk zzzz
    boolean success = CmdExecutor.execute(rootDir.getAbsolutePath(), "zip", "-r", zipFile.getAbsolutePath(),
            path);

    rootDir.delete();
    if (success) {
        return true;
    }

    File tmpFile = new File(zipFile.getParentFile(), zipFile.getName() + "_tmp");
    ZipUtils.addFileToZipFile(zipFile, tmpFile, file, path, true);
    zipFile.delete();
    tmpFile.renameTo(zipFile);

    return true;
}

From source file:jeplus.util.LineEnds.java

/**
 * Replace line ends in the (text) file with the given string. This function creates a temporary file then delete and rename.
 * @param file//  w w w . j  a va  2  s . com
 * @param newLN 
 */
protected static void convertFile(File file, String newLN) {
    try {
        BufferedReader fr = new BufferedReader(new FileReader(file));
        File tempfile = new File(file.getAbsolutePath() + ".temp");
        PrintWriter fw = new PrintWriter(new FileWriter(tempfile));
        String line = fr.readLine();
        while (line != null) {
            fw.print(line);
            fw.print(newLN);
            line = fr.readLine();
        }
        fr.close();
        fw.close();
        file.delete();
        if (!tempfile.renameTo(file)) {
            throw new Exception("Cannot rename " + tempfile.getName() + " to " + file.getName());
        }
    } catch (Exception ex) {
        logger.error("Error converting file " + file.getAbsolutePath(), ex);
    }
}

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

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

From source file:com.redhat.rhn.testing.RhnBaseTestCase.java

protected static void createDirIfNotExists(File dir) {
    String error = "Could not create the following directory:[" + dir.getPath()
            + "] . Please create that directory before proceeding with the tests";
    if (dir.exists() && !dir.isDirectory()) {
        if (!dir.renameTo(new File(dir.getPath() + ".bak")) && !dir.delete()) {
            throw new RuntimeException(error);
        }/*from   w w w .ja v a2s.c  o  m*/
    }

    if (!dir.exists() && !dir.mkdirs()) {
        throw new RuntimeException(error);
    }
}