Java File Copy nio copy(File src, File dest)

Here you can find the source of copy(File src, File dest)

Description

copy

License

Open Source License

Declaration

@SuppressWarnings("resource")
    public static void copy(File src, File dest) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    @SuppressWarnings("resource")
    public static void copy(File src, File dest) throws IOException {
        if (!src.exists())
            return;
        if (!dest.exists())
            dest.mkdir();/*from  w w w.  j  a  v  a2  s. c  o m*/
        for (File f : src.listFiles()) {
            File target = new File(dest, f.getName());
            if (f.isDirectory())
                copy(f, target);
            if (f.isFile()) {
                FileChannel ic = new FileInputStream(f).getChannel();
                FileChannel oc = new FileOutputStream(target).getChannel();
                try {
                    ic.transferTo(0, ic.size(), oc);
                } finally {
                    ic.close();
                    oc.close();
                }
            }
        }
    }
}

Related

  1. copy(File source, File target, FilenameFilter filter)
  2. copy(File sourceFile, File destFile)
  3. copy(File sourceFile, File destFile)
  4. copy(File sourceFile, File destinationFile)
  5. copy(File sourceFile, File targetFile)
  6. copy(File src, File dest)
  7. copy(File src, File dest)
  8. copy(File src, File dest)
  9. copy(File src, File dist)