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