Java File Rename renameFile(String currentPath, String newPath)

Here you can find the source of renameFile(String currentPath, String newPath)

Description

Renames a file

License

LGPL

Parameter

Parameter Description
currentPath currentPath/name of file
newPath new Path/name of file

Return

true if file was renamed, otherwise false

Declaration

public static boolean renameFile(String currentPath, String newPath) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

public class Main {
    /**//from  www.j  av  a2  s. c  om
     * Renames a file
     * 
     * @param currentPath
     *            currentPath/name of file
     * @param newPath
     *            new Path/name of file
     * @return true if file was renamed, otherwise false
     */
    public static boolean renameFile(String currentPath, String newPath) {
        boolean succeeded = false;
        try {
            File currentFile = new File(currentPath);
            if (currentFile.exists()) {
                File newFile = new File(newPath);
                succeeded = currentFile.renameTo(newFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return succeeded;
    }

    /**
     * Determines whether specified path exists
     * 
     * @param path
     * @return true if exists
     */
    public static boolean exists(String path) {
        File file = new File(path);
        return file.exists();
    }
}

Related

  1. renameFile(File srcFile, File destFile)
  2. renameFile(File srcFile, File dstFile)
  3. renameFile(File srcFile, File renameToFile)
  4. renameFile(final File path, final String fname)
  5. renameFile(final String oldName, final String newName)
  6. renameFile(String file, String toFile)
  7. renameFile(String fileFrom, String fileTo)
  8. renameFile(String fileName, String destFilename)
  9. renameFile(String filePath, String descFilePath)