Android File Move move(File file, File directory)

Here you can find the source of move(File file, File directory)

Description

Moves file into directory.

License

Open Source License

Parameter

Parameter Description
file the file to move
directory the directory to move file into

Exception

Parameter Description
IllegalArgumentException if file is Check#validFile not valid;directory is Check#validDirectory not valid; rename finds an issue
SecurityException if a security manager exists and denies write access to file or directory
IOException if an I/O problem occurs; this includes failure of the file to be renamed

Return

a new File instance whose path is for the new location of file after the move

Declaration

public static File move(File file, File directory)
        throws IllegalArgumentException, SecurityException, IOException 

Method Source Code

/*//from  w ww. ja  va 2 s .c  o  m
Copyright ? 2008 Brent Boyer

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License along with this program (see the license directory in this project).  If not, see <http://www.gnu.org/licenses/>.
 */

import bb.science.FormatUtil;
import bb.util.Check;
import bb.util.StringUtil;
import bb.util.ThrowableUtil;
import bb.util.logging.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.util.Random;
import java.util.logging.Level;
import org.junit.Assert;
import org.junit.Test;

public class Main{
    /**
     * Moves file into directory.
     * <p>
     * This is a convenience method that simply returns <code>{@link #rename rename}(file, new File(directory, file.getName())</code>.
     * <p>
     * @param file the file to move
     * @param directory the directory to move file into
     * @return a new File instance whose path is for the new location of file after the move
     * @throws IllegalArgumentException if file is {@link Check#validFile not valid};
     * directory is {@link Check#validDirectory not valid}; rename finds an issue
     * @throws SecurityException if a security manager exists and denies write access to file or directory
     * @throws IOException if an I/O problem occurs; this includes failure of the file to be renamed
     */
    public static File move(File file, File directory)
            throws IllegalArgumentException, SecurityException, IOException {
        Check.arg().validFile(file);
        Check.arg().validDirectory(directory);

        return rename(file, new File(directory, file.getName()));
    }
    /**
     * Trys to rename file1 to file2.
     * Since file2 may be in a different directory and/or have a different name,
     * this is really a combined move/rename method.
     * <p>
     * This method was written because {@link File#renameTo File.renameTo} unfortunately
     * merely returns a boolean indicating the success of the operation, which forces the user to check.
     * In contrast, this method corrects that defect and throws an Exception instead.
     * <p>
     * Furthermore, for maximum safety, this method will never overwrite an existing but different file.
     * Therefore, it insists that file2 must not currently exist unless it is equal to file1.
     * (One reason why the user may wish to supply file2 equal to file1 is if their
     * operating system has case insensitive file names:
     * perhaps they are simply trying to change file1's name to a standard case.)
     * <p>
     * Note: {@link DirUtil#rename DirUtil.rename} should be used to rename directories.
     * <p>
     * @param file1 the currently existing file
     * @param file2 the file that is to be renamed to
     * @return file2
     * @throws IllegalArgumentException if file1 is {@link Check#validFile not valid};
     * file2 == null; file2 already exists and is not equal to file1
     * @throws SecurityException if a security manager exists and denies write access to file1 or file2
     * @throws IOException if an I/O problem occurs; this includes failure of the file to be renamed
     */
    public static File rename(File file1, File file2)
            throws IllegalArgumentException, SecurityException, IOException {
        Check.arg().validFile(file1);
        Check.arg().notNull(file2);
        if (file2.exists() && !file1.equals(file2))
            throw new IllegalArgumentException("file2 = " + file2.getPath()
                    + " already exists and is not equal to file1 = "
                    + file1.getPath());

        boolean renamed = file1.renameTo(file2);
        if (!renamed)
            throw new IOException("failed to rename file1 = "
                    + file1.getPath() + " to file2 = " + file2.getPath());
        return file2;
    }
}

Related

  1. move(File source, File destination)
  2. move(String sourceFileName, String destinationFileName)
  3. moveFile(String sourceFileName, String destPath)
  4. moveSubFiles(String sourceFileName, String destPath)