Description
Renames the specified file, if the destination does not exist.
License
Open Source License
Parameter
Parameter | Description |
---|
pFrom | The file to rename |
pTo | The new file |
Exception
Return
true , if the file was renamed.
Declaration
public static boolean rename(File pFrom, File pTo) throws IOException
Method Source Code
//package com.java2s;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
/**//www .j a v a 2 s . com
* Renames the specified file, if the destination does not exist.
* If the destination is a directory (and the source is not), the source
* file is simply moved to the destination directory.
*
* @param pFrom The file to rename
* @param pTo The new file
* @return {@code true}, if the file was renamed.
* @throws java.io.IOException if rename fails
*/
public static boolean rename(File pFrom, File pTo) throws IOException {
return rename(pFrom, pTo, false);
}
/**
* Renames the specified file.
* If the destination is a directory (and the source is not), the source
* file is simply moved to the destination directory.
*
* @param pFrom The file to rename
* @param pTo The new file
* @param pOverWrite Specifies if the tofile should be overwritten, if it
* exists
* @return {@code true}, if the file was renamed.
*
* @throws FileNotFoundException if {@code pFrom} does not exist.
*/
public static boolean rename(File pFrom, File pTo, boolean pOverWrite) throws IOException {
if (!pFrom.exists()) {
throw new FileNotFoundException(pFrom.getAbsolutePath());
}
if (pFrom.isFile() && pTo.isDirectory()) {
pTo = new File(pTo, pFrom.getName());
}
return (pOverWrite || !pTo.exists()) && pFrom.renameTo(pTo);
}
/**
* Renames the specified file, if the destination does not exist.
* If the destination is a directory (and the source is not), the source
* file is simply moved to the destination directory.
*
* @param pFrom The file to rename
* @param pTo The new name of the file
* @return {@code true}, if the file was renamed.
* @throws java.io.IOException if rename fails
*/
public static boolean rename(File pFrom, String pTo) throws IOException {
return rename(pFrom, new File(pTo), false);
}
/**
* Renames the specified file.
* If the destination is a directory (and the source is not), the source
* file is simply moved to the destination directory.
*
* @param pFrom The file to rename
* @param pTo The new name of the file
* @param pOverWrite Specifies if the tofile should be overwritten, if it
* exists
* @return {@code true}, if the file was renamed.
* @throws java.io.IOException if rename fails
*/
public static boolean rename(File pFrom, String pTo, boolean pOverWrite) throws IOException {
return rename(pFrom, new File(pTo), pOverWrite);
}
/**
* Renames the specified file, if the destination does not exist.
* If the destination is a directory (and the source is not), the source
* file is simply moved to the destination directory.
*
* @param pFrom The name of the file to rename
* @param pTo The new name of the file
* @return {@code true}, if the file was renamed.
* @throws java.io.IOException if rename fails
*/
public static boolean rename(String pFrom, String pTo) throws IOException {
return rename(new File(pFrom), new File(pTo), false);
}
/**
* Renames the specified file.
* If the destination is a directory (and the source is not), the source
* file is simply moved to the destination directory.
*
* @param pFrom The name of the file to rename
* @param pTo The new name of the file
* @param pOverWrite Specifies if the tofile should be overwritten, if it
* exists
* @return {@code true}, if the file was renamed.
* @throws java.io.IOException if rename fails
*/
public static boolean rename(String pFrom, String pTo, boolean pOverWrite) throws IOException {
return rename(new File(pFrom), new File(pTo), pOverWrite);
}
}
Related
- rename(File from, File to)
- rename(File from, File to)
- rename(File from, File to)
- rename(File from, File to, boolean overwrite)
- rename(File oldFile, File newFile)
- rename(File pFrom, File pTo, boolean pOverWrite)
- rename(File source, File dest)
- rename(File source, File target)
- rename(File src, File dest)