Android File Rename renameFile(String origPath, String destPath)

Here you can find the source of renameFile(String origPath, String destPath)

Description

Convenience function to rename (move) a file.

License

Open Source License

Parameter

Parameter Description
origPath Original path
destPath Destination path

Exception

Parameter Description
IOException If the operation fails.

Declaration

public static void renameFile(String origPath, String destPath)
        throws IOException 

Method Source Code

//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()));
    }
}

Related

  1. rename(File fromFile, File toFile)
  2. rename(String from, String to)
  3. rename(String oldName, String newName)
  4. renameAs(String pathDir, String name, String wishName)
  5. renameFile(File source, File dest)
  6. renameFile(String source, String dest)
  7. renameFile(String strSrc, String strDest)
  8. renameFiles(File file, String filterRegex, String replaceRegex, String replacement)
  9. renameFiles(File file, String regex, String replacement)