Description
Copies a file to a new location.
License
Open Source License
Parameter
Parameter | Description |
---|
srcFile | an existing file to copy, must not be <code>null</code> |
destFile | the new file, must not be <code>null</code> |
preserveFileDate | true if the file date of the copy should be the same as the original |
Exception
Parameter | Description |
---|
NullPointerException | if source or destination is <code>null</code> |
IOException | if an IO error occurs during copying |
Declaration
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException
Method Source Code
//package com.java2s;
/*//w w w . jav a 2 s.c o m
* $Id$
* --------------------------------------------------------------------------------------
* Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
public class Main {
/**
* Copies a file to a new location.
* <p>
* This method copies the contents of the specified source file to the specified
* destination file. The directory holding the destination file is created if it
* does not exist. If the destination file exists, then this method will
* overwrite it.
*
* @param srcFile an existing file to copy, must not be <code>null</code>
* @param destFile the new file, must not be <code>null</code>
* @param preserveFileDate true if the file date of the copy should be the same
* as the original
* @throws NullPointerException if source or destination is <code>null</code>
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
* @see #copyFileToDirectory(File, File, boolean)
*/
public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
if (srcFile == null) {
throw new NullPointerException("Source must not be null");
}
if (destFile == null) {
throw new NullPointerException("Destination must not be null");
}
if (srcFile.exists() == false) {
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' exists but is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");
}
if (destFile.getParentFile() != null && destFile.getParentFile().exists() == false) {
if (destFile.getParentFile().mkdirs() == false) {
throw new IOException("Destination '" + destFile + "' directory cannot be created");
}
}
if (destFile.exists() && destFile.canWrite() == false) {
throw new IOException("Destination '" + destFile + "' exists but is read-only");
}
doCopyFile(srcFile, destFile, preserveFileDate);
}
/**
* Internal copy file method.
*
* @param srcFile the validated source file, must not be <code>null</code>
* @param destFile the validated destination file, must not be <code>null</code>
* @param preserveFileDate whether to preserve the file date
* @throws IOException if an error occurs
*/
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' exists but is a directory");
}
FileChannel input = new FileInputStream(srcFile).getChannel();
try {
FileChannel output = new FileOutputStream(destFile).getChannel();
try {
output.transferFrom(input, 0, input.size());
} finally {
closeQuietly(output);
}
} finally {
closeQuietly(input);
}
if (srcFile.length() != destFile.length()) {
throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
}
if (preserveFileDate) {
destFile.setLastModified(srcFile.lastModified());
}
}
/**
* Unconditionally close a <code>Channel</code>.
* <p>
* Equivalent to {@link Channel#close()}, except any exceptions will be ignored.
* This is typically used in finally blocks.
*
* @param channel the Channel to close, may be null or already closed
*/
public static void closeQuietly(Channel channel) {
try {
if (channel != null) {
channel.close();
}
} catch (IOException ioe) {
// ignore
}
}
}
Related
- copyFile(File src, File dst)
- copyFile(File src, File dst)
- copyFile(File src, File target)
- copyFile(File src, File target)
- copyFile(File srcFile, File destFile, boolean preserveFileDate)
- copyFile(File srcFile, File destFile, boolean preserveFileDate)
- copyFile(File srcFile, File dstFile, boolean overwrite)
- copyFile(FileChannel in, FileChannel out)
- copyFile(FileInputStream fromFile, FileOutputStream toFile)