Here you can find the source of renameFile(String origPath, String destPath)
Parameter | Description |
---|---|
origPath | Original path |
destPath | Destination path |
Parameter | Description |
---|---|
IOException | If the operation fails. |
public static void renameFile(String origPath, String destPath) throws IOException
//License from project: Open Source License import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.nio.channels.FileChannel; import javax.swing.filechooser.FileSystemView; import codebase.os.SysUtil; public class Main{ /**//from w ww .j a v a2s . c o m * Convenience function to rename (move) a file. * <p> * The rename works even if the destination file exists, however the operation is not * atomic on Windows, which means that it's possible for the rename to fail in such a * way that the destination file is deleted but the original is not renamed. * * @param origPath Original path * @param destPath Destination path * @throws IOException If the operation fails. */ public static void renameFile(String origPath, String destPath) throws IOException { File orig = new File(origPath); File dest = new File(destPath); // On windows, we must manually delete the destination file if it exists if (SysUtil.getOperatingSystem() == SysUtil.OS.WINDOWS && dest.exists()) dest.delete(); if (!orig.renameTo(dest)) throw new IOException(String.format( "Unable to rename file from '%1$s' to '%2$s'", orig.toString(), dest.toString())); } }