Java Utililty Methods File Copy nio

List of utility methods to do File Copy nio

Description

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

Method

booleancopy(File a, File b)
copy
FileChannel fc1 = new FileInputStream(a).getChannel();
if (b.exists())
    b.delete();
FileChannel fc2 = new FileOutputStream(b).getChannel();
long count1 = fc1.size();
fc1.transferTo(0, count1, fc2);
fc1.close();
fc2.close();
...
voidcopy(File base_path, File in, File out)
copy
File[] l = in.listFiles();
for (File f : l) {
    if (f.isDirectory()) {
        copy(base_path, f, out);
    } else {
        String relative_out = f.getAbsolutePath().replace(base_path.getAbsolutePath(), "");
        File dest_f = new File(out.getAbsolutePath() + relative_out);
        dest_f.mkdirs();
...
voidcopy(File copied, File destination)
copy
try {
    Files.copy(copied.toPath(), destination.toPath());
} catch (IOException e) {
    throw new Exception(copied.toString() + ": Can't copy");
Filecopy(File file, File toDirectory)
Copies a file to the specified directory.
if (file == null || !file.exists() || !file.canRead()) {
    throw new IOException("Unable to find source file to be copied " + file);
String basename = file.getName();
File destFile = new File(toDirectory, basename.toString());
if (!toDirectory.exists())
    toDirectory.createNewFile();
FileChannel fcSrc = null;
...
voidcopy(File from, File to)
copy
copy(from.toURI().toURL(), to);
voidcopy(File from, File to)
copy
if (!to.exists())
    to.createNewFile();
FileChannel fromChan = null;
FileChannel toChan = null;
try {
    fromChan = new FileInputStream(from).getChannel();
    toChan = new FileOutputStream(to).getChannel();
    toChan.transferFrom(fromChan, 0, fromChan.size());
...
voidcopy(File from, File to)
copy
try (FileInputStream fin = new FileInputStream(from);
        FileChannel in = fin.getChannel();
        FileOutputStream fout = new FileOutputStream(to);
        FileChannel out = fout.getChannel()) {
    in.transferTo(0, from.length(), out);
} catch (IOException e) {
    throw new RuntimeException(e);
voidcopy(File from, File to, final boolean recursive, Function excluded)
copy
final Path source = from.toPath();
final Path target = to.toPath();
if (recursive) {
    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
            new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
...
voidcopy(File in, File out)
copy
if (in.exists() && in != null && out != null) {
    if (!out.exists()) {
        if (in.isDirectory()) {
            out.mkdirs();
        } else {
            out.createNewFile();
    String source = in.isDirectory() ? "directory" : "file";
    String target = out.isDirectory() ? "directory" : "file";
    if (!source.equals(target)) {
        throw new IOException("Can't duplicate " + source + " as " + target);
    } else {
        if (source.equals("directory")) {
            File[] files = in.listFiles();
            for (File file : files) {
                copy(file, new File(out, file.getName()));
        } else {
            FileChannel inCh = new FileInputStream(in).getChannel();
            FileChannel outCh = new FileOutputStream(out).getChannel();
            inCh.transferTo(0, inCh.size(), outCh);
voidcopy(File in, File out, boolean overwrite)
Copy a file
if (overwrite || !out.exists() || (out.lastModified() < in.lastModified())) {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
...