Java Rename File rename(File pFrom, File pTo)

Here you can find the source of rename(File pFrom, File pTo)

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

Parameter Description

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

  1. rename(File from, File to)
  2. rename(File from, File to)
  3. rename(File from, File to)
  4. rename(File from, File to, boolean overwrite)
  5. rename(File oldFile, File newFile)
  6. rename(File pFrom, File pTo, boolean pOverWrite)
  7. rename(File source, File dest)
  8. rename(File source, File target)
  9. rename(File src, File dest)