Java Utililty Methods File Rename

List of utility methods to do File Rename

Description

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

Method

booleanrenameFile(String oldname, String newone)
Renames a file from oldname to newone.
boolean result = false;
File file = new File(oldname);
if (file.exists()) {
    result = file.renameTo(new File(newone));
return result;
voidrenameFile(String oldPath, String newName)
rename File
File file = new File(oldPath);
if (!file.exists()) {
    return;
String newPath = newName;
if (file.getParent() != null) {
    newPath = file.getParent() + File.separator + newName;
file.renameTo(new File(newPath));
voidrenameFile(String oldPath, String newPath)
rename File
try {
    File dest = null;
    if (!oldPath.equals(newPath)) {
        File file = new File(oldPath);
        dest = new File(newPath);
        file.renameTo(dest);
} catch (Exception e) {
...
voidrenameFile(String oldPath, String newPath, boolean overwrite)
Rename a file from an old path to a new path.
boolean done = false;
File source = new File(oldPath);
File destination = new File(newPath);
try {
    if (destination.exists()) {
        if (!overwrite) {
            throw new IOException("'" + newPath + "' already exists");
        if (!destination.delete()) {
            throw new IOException("Could not delete '" + newPath + "'");
    if (!source.renameTo(destination)) {
        throw new IOException("'" + oldPath + "' could not be renamed to '" + newPath + "'");
    done = true;
} finally {
    if (done) {
        source.delete();
booleanrenameFile(String origPath, String destPath)
Rename a file.
File orig = new File(origPath);
File dest = new File(destPath);
if (!orig.exists() || dest.exists())
    return false;
return orig.renameTo(dest);
booleanrenameFile(String path, String newPath)
rename File
synchronized (path.intern()) {
    File newFile = new File(newPath);
    if (newFile.exists()) {
        return false;
    File oldFile = new File(path);
    return oldFile.renameTo(newFile);