Here you can find the source of renameFile(String fromFilename, String toFilename)
public static void renameFile(String fromFilename, String toFilename) throws IOException
//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 + "."); } } }