Description
Copies a File source file to a File destination file .
License
Apache License
Parameter
Parameter | Description |
---|
src | the File source file , which must be non-null and readable |
dest | the File destination file , which must be non-null and writable |
Exception
Parameter | Description |
---|
IOException | thrown if<ul><li> src or dest is null</li><li> src is not readable</li><li> dest is not writeable</li></ul> |
Declaration
public static void copyFile(final File src, final File dest) throws IOException
Method Source Code
//package com.java2s;
//License from project: Apache License
import java.io.*;
import java.nio.channels.FileChannel;
import java.util.*;
import static java.lang.String.format;
public class Main {
private static int ONE_MEGABYTE = 1024 * 1024;
/**//from w w w. j a v a 2s. c om
* Copies a {@link File source file} to a {@link File destination file}.
*
* @param src the {@link File source file}, which must be non-null and
* readable
* @param dest the {@link File destination file}, which must be non-null
* and
* writable
* @throws IOException Thrown if an IO error occurred copying the
* {@link File src} to {@link File dest}
* @throws IOException thrown if
* <ul>
* <li>{@code src} or {@code dest} is {@code null}</li>
* <li>{@code src} is not readable</li>
* <li>{@code dest} is not writeable</li>
* </ul>
*/
public static void copyFile(final File src, final File dest) throws IOException {
if (!readable(src)) {
throw new IOException(format("%s: unreadable", src));
}
if (!writable(dest)) {
throw new IOException(format("%s: unwritable", src));
}
// initialize streams/channels
FileInputStream srcInput = null;
FileChannel srcChannel = null;
FileOutputStream destOutput = null;
FileChannel destChannel = null;
try {
srcInput = new FileInputStream(src);
srcChannel = srcInput.getChannel();
destOutput = new FileOutputStream(dest);
destChannel = destOutput.getChannel();
// transfer from src to dest channel, one megabyte at a time
long size = srcChannel.size();
long pos = 0;
long count = 0;
while (pos < size) {
count = (size - pos) > ONE_MEGABYTE ? ONE_MEGABYTE : (size - pos);
pos += destChannel.transferFrom(srcChannel, pos, count);
}
} finally {
// close channels and streams
if (destChannel != null) {
destChannel.close();
}
if (destOutput != null) {
destOutput.close();
}
if (srcChannel != null) {
srcChannel.close();
}
if (srcInput != null) {
srcInput.close();
}
}
}
/**
* Returns {@code true} if every {@link File} in {@code files} is non-null
* and can be read, {@code false} otherwise.
*
* @param files {@code File[]}; may be null
* @return boolean
*/
public static boolean readable(final File... files) {
if (!hasItems(files)) {
return false;
}
boolean readable = true;
for (File f : files) {
readable &= f.canRead();
}
return readable;
}
/**
* Returns {@code true} if {@code file} is non-null and can be written,
* {@code false} otherwise.
*
* @param file {@link File}; may be null
* @return boolean
*/
public static boolean writable(final File file) {
if (file != null && file.canWrite())
return true;
return false;
}
/**
* Returns {@code true} if the collection is non-null and non-empty,
* {@code false} otherwise.
*
* @param c Collection, may be null
* @return boolean
*/
public static boolean hasItems(final Collection<?> c) {
return c != null && !c.isEmpty();
}
/**
* Returns {@code true} if the map is non-null and is non-empty,
* {@code false} otherwise.
*
* @param <K> Captured key type
* @param <V> Captured value type
* @param m Map of type {@code <K, V>}, may be null
* @return boolean
*/
public static <K, V> boolean hasItems(final Map<K, V> m) {
return m != null && !m.isEmpty();
}
/**
* Returns {@code true} if the array is non-null and has a length greater
* than zero, {@code false} otherwise.
*
* @param <T> Captured array type
* @param t Array of type {@code <T>}
* @return boolean
*/
public static <T> boolean hasItems(final T[] t) {
return t != null && t.length > 0;
}
}
Related
- copyFile(FileChannel in, FileChannel out)
- copyFile(FileInputStream fromFile, FileOutputStream toFile)
- copyFile(final File in, final File out)
- copyFile(final File in, final File out)
- copyFile(final File input, final File output)
- copyFile(final File src, final File dst)
- copyFile(final File srcFile, final File destFile)
- copyFile(final File srcFile, final File destFile)
- copyFile(final String src, final String dest)