Java Utililty Methods Rename File

List of utility methods to do Rename File

Description

The list of methods to do Rename File are organized into topic(s).

Method

voidrenameTo(File from, File to, boolean deleteDestination)
rename To
if (deleteDestination) {
    deleteIfExists(to);
if (!from.renameTo(to)) {
    throw new IOException();
voidrenameTo(File fromFile, File toFile)
rename a file to another file and throw runtime exception if not ok
if (!fromFile.renameTo(toFile)) {
    throw new RuntimeException("Cannot rename file: '" + fromFile.getAbsolutePath() + "', to file: '"
            + toFile.getAbsolutePath() + "'");
booleanrenameTo(File orig, File dest)
rename To
if (orig.equals(dest))
    throw new IllegalArgumentException("Huh? the two file descriptors are the same!");
if (!orig.exists()) {
    throw new IllegalArgumentException("Original doesn't exist!");
if (!orig.renameTo(dest)) {
    if (!dest.delete()) {
        if (dest.exists()) {
...
FilerenameTo(File source, String newName)
Rename the file specified to the new name specified in parameters.
if (!source.exists())
    return null;
String parentPath = source.getParent() == null ? "" : source.getParent();
File target = new File(parentPath, newName);
boolean ret = source.renameTo(target);
return ret ? target : null;
booleanrenameTo(final File srcFile, final File dstfile)
Rename File in privileged mode.
return new Boolean(srcFile.renameTo(dstfile));
booleanrenameTo(String fileName, String targetFileName)
Renames a file with new file name.
File current = new File(fileName);
File newF = new File(targetFileName);
if (!current.exists()) {
    throw new IOException("File [" + fileName + "] does not exits");
if (newF.exists()) {
    newF.delete();
return current.renameTo(new File(targetFileName));
booleanrenameToBackupName(File file)
rename To Backup Name
return file.renameTo(backupFile(file));
voidrenameToBackupName(File file)
rename To Backup Name
File backupFile = new File(file.getParentFile(), file.getName() + "~");
if (backupFile.exists()) {
    boolean deleted = backupFile.delete();
    if (!deleted) {
        throw new IOException("Cannot delete backup file " + backupFile);
boolean renamed = file.renameTo(backupFile);
...
FilerenameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
rename To Next Sequenced File
synchronized (renameToNextSequencedFileLock) {
    File src = new File(srcfile);
    File dest = null;
    int count = 0;
    if (destfolder == null) {
        if (src.getParent() != null)
            destfolder = src.getAbsoluteFile().getParent();
        else
...
FilerenameToTempDir(File file, String newName)
rename To Temp Dir
return renameToTempDir(file, newName, null, null, 1);