Java File Rename renameFile(String fromFilename, String toFilename)

Here you can find the source of renameFile(String fromFilename, String toFilename)

Description

rename File

License

Open Source License

Declaration

public static void renameFile(String fromFilename, String toFilename) throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  ww . ja v  a 2  s. c  om*/
 * This software source code is provided by the USEF Foundation. The copyright
 * and all other intellectual property rights relating to all software source
 * code provided by the USEF Foundation (and changes and modifications as well
 * as on new versions of this software source code) belong exclusively to the
 * USEF Foundation and/or its suppliers or licensors. Total or partial
 * transfer of such a right is not allowed. The user of the software source
 * code made available by USEF Foundation acknowledges these rights and will
 * refrain from any form of infringement of these rights.
 *
 * The USEF Foundation provides this software source code "as is". In no event
 * shall the USEF Foundation and/or its suppliers or licensors have any
 * liability for any incidental, special, indirect or consequential damages;
 * loss of profits, revenue or data; business interruption or cost of cover or
 * damages arising out of or in connection with the software source code or
 * accompanying documentation.
 *
 * For the full license agreement see http://www.usef.info/license.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    public static void renameFile(String fromFilename, String toFilename) throws IOException {
        File fileFrom = new File(fromFilename);
        File fileTo = new File("newname");
        if (fileTo.exists()) {
            throw new java.io.IOException("File " + toFilename + " already exists! Could not rename it.");
        }

        boolean success = fileFrom.renameTo(fileTo);
        if (!success) {
            throw new java.io.IOException("Could not rename file " + toFilename + " to " + fromFilename + ".");
        }
    }
}

Related

  1. renameFile(String file, String toFile)
  2. renameFile(String fileFrom, String fileTo)
  3. renameFile(String fileName, String destFilename)
  4. renameFile(String filePath, String descFilePath)
  5. renameFile(String from, String to, String baksuffix)
  6. renameFile(String newName, String oldName)
  7. renameFile(String oldFile, String newName)
  8. renameFile(String oldFileName, String newFileName)
  9. renameFile(String oldname, String newname)