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:com.puzzle.module.FileFilterAndExcutor.java

public boolean accept(File f) {
    if (f.getName().matches("^.*.xml$") && f.renameTo(f)) {
        //               this. sendExe.excute(f);
        return true;
    }//  w w  w.ja  v  a 2 s .c om

    return false;
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();/*from  w  w  w. java2s. co m*/
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:com.splunk.shuttl.archiver.importexport.csv.CsvBucketCreator.java

private void moveCsvFileToBucketDir(File csvFile, File bucketFile) {
    csvFile.renameTo(new File(bucketFile, csvFile.getName()));
}

From source file:com.dmbstream.android.util.Util.java

public static void atomicCopy(File from, File to) throws IOException {
    FileInputStream in = null;/*from   ww w  . j ava 2 s  .  com*/
    FileOutputStream out = null;
    File tmp = null;
    try {
        tmp = new File(to.getPath() + ".tmp");
        in = new FileInputStream(from);
        out = new FileOutputStream(tmp);
        in.getChannel().transferTo(0, from.length(), out.getChannel());
        out.close();
        if (!tmp.renameTo(to)) {
            throw new IOException("Failed to rename " + tmp + " to " + to);
        }
        Log.i(TAG, "Copied " + from + " to " + to);
    } catch (IOException x) {
        close(out);
        delete(to);
        throw x;
    } finally {
        close(in);
        close(out);
        delete(tmp);
    }
}

From source file:graphene.util.fs.FileUtils.java

public static boolean renameFile(final File srcFile, final File renameToFile) {
        if (!srcFile.exists()) {
            throw new NotFoundException("Source file[" + srcFile.getName() + "] not found");
        }//  w w w .  j  a  va 2  s  .  c  om
        if (renameToFile.exists()) {
            throw new NotFoundException("Target file[" + renameToFile.getName() + "] already exists");
        }
        if (!renameToFile.getParentFile().isDirectory()) {
            throw new NotFoundException("Target directory[" + renameToFile.getParent() + "] does not exists");
        }
        int count = 0;
        boolean renamed;
        do {
            renamed = srcFile.renameTo(renameToFile);
            if (!renamed) {
                count++;
                waitSome();
            }
        } while (!renamed && (count <= WINDOWS_RETRY_COUNT));
        return renamed;
    }

From source file:com.couchbase.lite.Manager.java

private static void moveSQLiteDbFiles(String oldDbPath, String newDbPath) {
    for (String suffix : Arrays.asList("", "-wal", "-shm", "-journal")) {
        File oldFile = new File(oldDbPath + suffix);
        if (!oldFile.exists())
            continue;
        if (newDbPath != null)
            oldFile.renameTo(new File(newDbPath + suffix));
        else//from  w w w  .j av  a  2  s  . c  o m
            oldFile.delete();
    }
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void writeObject(Object o, File file) {
    File tmpFile = new File(file.getParent(), file.getName() + ".tmp");

    try {// w  w w  .j a v a2  s. com
        final FileOutputStream fous = new FileOutputStream(tmpFile);
        try {
            ObjectOutput output = new ObjectOutputStream(fous);
            output.writeObject(o);
        } finally {
            Utils.closeQuietly(fous);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    if (!tmpFile.renameTo(file)) {
        throw new RuntimeException(
                format("Could not rename [%s] to [%s]", tmpFile.getAbsolutePath(), file.getAbsolutePath()));
    }
}

From source file:com.metamx.druid.loading.MMappedQueryableIndexFactory.java

@Override
public QueryableIndex factorize(File parentDir) throws SegmentLoadingException {
    try {//from ww  w  .ja  v a  2 s  .  co  m
        if (!IndexIO.canBeMapped(parentDir)) {
            File canBeMappedDir = new File(parentDir, "forTheMapping");
            if (canBeMappedDir.exists()) {
                FileUtils.deleteDirectory(canBeMappedDir);
            }
            canBeMappedDir.mkdirs();

            IndexIO.storeLatest(IndexIO.readIndex(parentDir), canBeMappedDir);
            if (!IndexIO.canBeMapped(canBeMappedDir)) {
                throw new SegmentLoadingException("WTF!? newly written file[%s] cannot be mapped!?",
                        canBeMappedDir);
            }
            for (File file : canBeMappedDir.listFiles()) {
                if (!file.renameTo(new File(parentDir, file.getName()))) {
                    throw new SegmentLoadingException("Couldn't rename[%s] to [%s]", canBeMappedDir, parentDir);
                }
            }
            FileUtils.deleteDirectory(canBeMappedDir);
        }

        return IndexIO.loadIndex(parentDir);
    } catch (IOException e) {
        log.warn(e, "Got exception!!!! Going to delete parentDir[%s]", parentDir);
        try {
            FileUtils.deleteDirectory(parentDir);
        } catch (IOException e2) {
            log.error(e, "Problem deleting parentDir[%s]", parentDir);
        }
        throw new SegmentLoadingException(e, "%s", e.getMessage());
    }
}

From source file:com.liferay.ide.theme.core.util.BuildHelper.java

/**
 * Safe rename. Will try multiple times before giving up.
 *
 * @param from/*w ww . j  ava  2 s .  com*/
 * @param to
 * @param retrys
 *            number of times to retry
 * @return <code>true</code> if it succeeds, <code>false</code> otherwise
 */
private static boolean safeRename(File from, File to, int retrys) {
    // make sure parent dir exists
    File dir = to.getParentFile();
    if (dir != null && !dir.exists())
        dir.mkdirs();

    int count = 0;
    while (count < retrys) {
        if (from.renameTo(to))
            return true;

        count++;
        // delay if we are going to try again
        if (count < retrys) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                // ignore
            }
        }
    }
    return false;
}

From source file:com.metamx.druid.loading.ConvertingBaseQueryableFactory.java

@Override
public StorageAdapter factorize(File parentDir) throws StorageAdapterLoadingException {
    File indexFile = new File(parentDir, "index.drd");
    if (!indexFile.exists()) {
        throw new StorageAdapterLoadingException("indexFile[%s] does not exist.", indexFile);
    }//from  ww w  . j a  va  2  s.  c o m

    try {
        if (!IndexIO.canBeMapped(parentDir)) {
            File canBeMappedDir = new File(parentDir, "forTheMapping");
            if (canBeMappedDir.exists()) {
                FileUtils.deleteDirectory(canBeMappedDir);
            }
            canBeMappedDir.mkdirs();

            IndexIO.storeLatest(IndexIO.readIndex(parentDir), canBeMappedDir);
            if (!IndexIO.canBeMapped(canBeMappedDir)) {
                throw new StorageAdapterLoadingException("WTF!? newly written file[%s] cannot be mapped!?",
                        canBeMappedDir);
            }
            for (File file : canBeMappedDir.listFiles()) {
                if (!file.renameTo(new File(parentDir, file.getName()))) {
                    throw new StorageAdapterLoadingException("Couldn't rename[%s] to [%s]", canBeMappedDir,
                            indexFile);
                }
            }
            FileUtils.deleteDirectory(canBeMappedDir);
        }

        return factorizeConverted(parentDir);
    } catch (IOException e) {
        log.warn(e, "Got exception, deleting index[%s]", indexFile);
        try {
            FileUtils.deleteDirectory(parentDir);
        } catch (IOException e2) {
            log.error(e, "Problem deleting parentDir[%s]", parentDir);
        }
        throw new StorageAdapterLoadingException(e, e.getMessage());
    }
}