Java Rename File renameTo(String fileName, String targetFileName)

Here you can find the source of renameTo(String fileName, String targetFileName)

Description

Renames a file with new file name.

License

Open Source License

Parameter

Parameter Description
fileName the file name (including path) to rename
targetFileName the target file name (including path)

Exception

Parameter Description
IOException an exception

Return

true if and only if the renaming succeeded

Declaration

public static boolean renameTo(String fileName, String targetFileName) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w w w.ja v a2  s.c  o m*/
 * FileUtils.java
 *
 * Developed by The Medici Archive Project Inc. (2010-2012)
 * 
 * This file is part of DocSources.
 * 
 * DocSources 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 2 of the License, or
 * (at your option) any later version.
 * 
 * DocSources 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Renames a file with new file name. If the target file exists it is overridden.
     * 
     * @param fileName the file name (including path) to rename
     * @param targetFileName the target file name (including path) 
     * @return true if and only if the renaming succeeded
     * @throws IOException
     */
    public static boolean renameTo(String fileName, String targetFileName) throws IOException {
        File current = new File(fileName);
        File newF = new File(targetFileName);

        if (!current.exists()) {
            throw new IOException("File [" + fileName + "] does not exits");
        }

        if (newF.exists()) {
            newF.delete();
        }

        return current.renameTo(new File(targetFileName));
    }

    /**
     * Returns true if the file exists.
     * 
     * @param fileName the file name (including path) to check
     * @return true if the file exists
     * @throws IOException
     */
    public static boolean exists(String fileName) throws IOException {
        return new File(fileName).exists();
    }
}

Related

  1. renameTo(File from, File to, boolean deleteDestination)
  2. renameTo(File fromFile, File toFile)
  3. renameTo(File orig, File dest)
  4. renameTo(File source, String newName)
  5. renameTo(final File srcFile, final File dstfile)
  6. renameToBackupName(File file)
  7. renameToBackupName(File file)
  8. renameToNextSequencedFile(String srcfile, String destfolder, String prefix, String suffix)
  9. renameToTempDir(File file, String newName)