Java File Rename renameFile(String oldFile, String newName)

Here you can find the source of renameFile(String oldFile, String newName)

Description

Function renames a file to a new name.

License

Open Source License

Declaration

public static void renameFile(String oldFile, String newName) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  w w . j  a v a 2s  .c  o m*/
 * Copyright 2014 Elhuyar Fundazioa
    
This file is part of EliXa.
    
EliXa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
EliXa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with EliXa.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Function renames a file to a new name. if the new name already exists throws an exception.
     *  
     */
    public static void renameFile(String oldFile, String newName) throws IOException {
        File file1 = new File(oldFile);
        File file2 = new File(newName);
        if (file2.exists()) {
            throw new java.io.IOException("FileUtilsElh::renameFile : file exists");
        } else if (!file1.renameTo(file2)) {
            System.err.println("FileUtilsElh::renameFile : moving file failed\n\t" + file1.getAbsolutePath() + "\t"
                    + file2.getAbsolutePath());
        }
        // Rename file (or directory)
        //file1.renameTo(file2);       
    }
}

Related

  1. renameFile(String fileName, String destFilename)
  2. renameFile(String filePath, String descFilePath)
  3. renameFile(String from, String to, String baksuffix)
  4. renameFile(String fromFilename, String toFilename)
  5. renameFile(String newName, String oldName)
  6. renameFile(String oldFileName, String newFileName)
  7. renameFile(String oldname, String newname)
  8. renameFile(String oldname, String newname)
  9. renameFile(String oldname, String newone)