Java Rename File renameTo(File source, String newName)

Here you can find the source of renameTo(File source, String newName)

Description

Rename the file specified to the new name specified in parameters.

License

Open Source License

Parameter

Parameter Description
source The source file object.
newName The new name of the object.

Return

The new file object for the new path.

Declaration

public static File renameTo(File source, String newName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

public class Main {
    /**//  w  w  w .j  a v  a2  s .  c  o  m
     * Rename the file specified to the new name specified 
     * in parameters.<br>
     * 
     * @param source   The source file object.
     * @param newName   The new name of the object.
     * @return The new file object for the new path.
     */
    public static File renameTo(File source, String newName) {
        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;
    }
}

Related

  1. renameTempDMLScript(String dmlScriptFile)
  2. renameTo(File currentFile, String newName)
  3. renameTo(File from, File to, boolean deleteDestination)
  4. renameTo(File fromFile, File toFile)
  5. renameTo(File orig, File dest)
  6. renameTo(final File srcFile, final File dstfile)
  7. renameTo(String fileName, String targetFileName)
  8. renameToBackupName(File file)
  9. renameToBackupName(File file)