Java Utililty Methods Rename File

List of utility methods to do Rename File

Description

The list of methods to do Rename File are organized into topic(s).

Method

voidrename(File from, File to)
rename
ensureFileDoesNotExist(to);
if (!from.renameTo(to)) {
    throw new IOException("Unable to rename " + from + " to " + to);
voidrename(File from, File to, boolean overwrite)
rename
boolean success = true;
if (!from.exists()) {
    throw new IOException("file from=[" + from.getAbsolutePath() + "] not exists.");
if (to.exists()) {
    if (overwrite) {
        success = to.delete();
        if (!success) {
...
voidrename(File oldFile, File newFile)
rename
rename(oldFile, newFile, false);
booleanrename(File pFrom, File pTo)
Renames the specified file, if the destination does not exist.
return rename(pFrom, pTo, false);
booleanrename(File pFrom, File pTo, boolean pOverWrite)
Renames the specified file.
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);
voidrename(File source, File dest)
It seems that java doesn't like renaming files across partitions (i.e.
if (!source.renameTo(dest)) {
    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(source);
        to = new FileOutputStream(dest);
        stream(from, to);
    } finally {
...
booleanrename(File source, File target)
rename
if (source == null || target == null) {
    throw new IllegalArgumentException("Input cannot be null.");
return source.renameTo(target);
booleanrename(File src, File dest)
rename
if (src.exists()) {
    return src.renameTo(dest);
return false;
Filerename(File src, String name)
Renames a file using a string.
File ret = new File(src.getParentFile(), name);
if (src.renameTo(ret))
    return ret;
else
    return null;
voidrename(File srcFile, String newName)
rename.
if (srcFile == null) {
    throw new NullPointerException("Source must not be null");
if (newName == null || newName.length() == 0) {
    throw new NullPointerException("newName must not be null");
if (srcFile.exists() == false) {
    throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
...