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

voidrename(File aFile, File dest)
Renames the file aFile.
if (!aFile.renameTo(dest)) {
    throw new IOException("Failed to rename " + aFile + " to " + dest);
booleanrename(File f1, File f2)
rename
ensurePathExists(f2.getParent());
if (f2.exists())
    f2.delete();
return f1.renameTo(f2);
Filerename(File file, String newName, boolean overwite)
rename
File target = new File(file.getParentFile(), newName);
if (target.exists()) {
    if (overwite) {
        if (!target.delete())
            return null;
    } else {
        return null;
return file.renameTo(target) ? target : null;
voidrename(File file, String prefix, String name, HashMap optional)
rename
if (file.isFile()) {
    final String before = file.getName();
    final String shortName = prefix + file.getName();
    optional.put(before, shortName);
    file.renameTo(new File(name));
Filerename(File from)
rename
File to = new File(from.getParentFile(), from.getName() + "." + System.currentTimeMillis());
rename(from, to);
return to;
booleanrename(File from, File to)
rename
if (!from.renameTo(to)) {
    if (copy(from, to)) {
        if (!delete(from, true )) {
            return false;
    } else {
        return false;
return true;
voidrename(File from, File to)
rename file
if (to.exists() && !to.delete()) {
    throw new IOException("Failed to delete " + to + " while trying to rename " + from);
File parent = to.getParentFile();
if (parent != null && !parent.exists() && !parent.mkdirs()) {
    throw new IOException("Failed to create directory " + parent + " while trying to rename " + from);
if (!from.renameTo(to)) {
...
voidrename(File from, File to)
Renames file, with checking errors and 3 seconds retry against external programs (like antivirus or TortoiseSVN) locking.
if (!from.exists()) {
    throw new IOException("Source file to rename (" + from + ") doesn't exist");
if (to.exists()) {
    throw new IOException("Target file to rename (" + to + ") already exists");
long bfor = System.currentTimeMillis();
while (!from.renameTo(to)) {
...
voidrename(File from, File to)
rename
if (!from.exists()) {
    throw new Exception("Rename failed as " + from + " does not exists");
if (to.exists()) {
    throw new Exception("Rename failed as " + to + " already exists");
from.renameTo(to);
for (int i = 0; from.exists() && !to.exists() && i < 20; i++) {
...
voidrename(File from, File to)
Atomically renames one file to another, throwing IOException when unsuccessful.
if (!from.renameTo(to))
    throw new IOException("Unable to atomically rename \"" + from + "\" to \"" + to + '"');