Java File Rename renameFile(String oldname, String newname)

Here you can find the source of renameFile(String oldname, String newname)

Description

rename File

License

Open Source License

Declaration

public static boolean renameFile(String oldname, String newname) 

Method Source Code

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

import java.io.*;

public class Main {
    public static boolean renameFile(String oldname, String newname) {
        // File (or directory) with old name
        File file = new File(oldname);

        // File (or directory) with new name
        File file2 = new File(newname);
        if (file2.exists()) {
            System.out/*from ww w . j  av a 2  s  .co m*/
                    .println("can not rename since target name is same as source name");
            return false;
        }

        // Rename file (or directory)
        boolean success = file.renameTo(file2);
        if (!success) {
            System.out.println("can not rename");
        }
        return true;
    }

    public static boolean renameFile(File oldFile, File newFile) {
        if (newFile.exists()) {
            System.out
                    .println("can not rename since target name is same as source name");
            return false;
        }
        boolean success = oldFile.renameTo(newFile);
        if (!success) {
            System.out.println("can not rename");
        }
        return true;
    }
}

Related

  1. renameFile(String from, String to, String baksuffix)
  2. renameFile(String fromFilename, String toFilename)
  3. renameFile(String newName, String oldName)
  4. renameFile(String oldFile, String newName)
  5. renameFile(String oldFileName, String newFileName)
  6. renameFile(String oldname, String newname)
  7. renameFile(String oldname, String newone)
  8. renameFile(String oldPath, String newName)
  9. renameFile(String oldPath, String newPath)