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

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

Description

Copy a file from the source file to the destination file.

License

Open Source License

Parameter

Parameter Description
source the source file or directory
dest the destination file or directory.

Exception

Parameter Description
IOException an exception

Declaration

public static void copy(File source, File dest) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.channels.FileChannel;

public class Main {
    public static final long MAX_TRANSFER_SIZE = Long.getLong("gemfire.FileUtil.MAX_TRANSFER_SIZE", 1024 * 1024)
            .longValue();//from w  w  w  .  ja v a  2s .co m
    public static final boolean USE_NIO = !Boolean.getBoolean("gemfire.FileUtil.USE_OLD_IO");

    /**
     * Copy a file from the source file to the destination file.
     * If the source is a directory, it will be copied recursively.
     * 
     * Note that unlike unix cp, if the destination is directory,
     * the source *contents* will be copied to the destination *contents*, 
     * not as a subdirectory of dest.
     * 
     * @param source the source file or directory
     * @param dest the destination file or directory.
     * @throws IOException
     */
    public static void copy(File source, File dest) throws IOException {
        if (source.isDirectory()) {
            dest.mkdir();
            for (File child : listFiles(source)) {
                copy(child, new File(dest, child.getName()));
            }
        } else {
            if (source.exists()) {
                long lm = source.lastModified();
                if (dest.isDirectory()) {
                    dest = new File(dest, source.getName());
                }
                FileOutputStream fos = new FileOutputStream(dest);
                try {
                    FileInputStream fis = new FileInputStream(source);
                    try {
                        if (USE_NIO) {
                            nioCopy(fos, fis);
                        } else {
                            oioCopy(source, fos, fis);
                        }
                    } finally {
                        fis.close();
                    }
                } finally {
                    fos.close();
                }
                dest.setExecutable(source.canExecute(), true);
                dest.setLastModified(lm);
            }
        }
    }

    /**
     * Copy a URL to a file.
     * @throws IOException
     */
    public static void copy(URL url, File file) throws IOException {
        InputStream is = url.openStream();
        try {
            OutputStream os = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[8192];
                int read;
                while ((read = is.read(buffer)) > 0) {
                    os.write(buffer, 0, read);
                }
            } finally {
                os.close();
            }
        } finally {
            is.close();
        }

    }

    /**
     * Basically just like {@link File#listFiles()} but instead of returning null
     * returns an empty array. This fixes bug 43729
     */
    public static File[] listFiles(File dir) {
        File[] result = dir.listFiles();
        if (result == null) {
            result = new File[0];
        }
        return result;
    }

    /**
     * Basically just like {@link File#listFiles(FilenameFilter)} but instead of returning null
     * returns an empty array. This fixes bug 43729
     */
    public static File[] listFiles(File dir, FilenameFilter filter) {
        File[] result = dir.listFiles(filter);
        if (result == null) {
            result = new File[0];
        }
        return result;
    }

    /**
     * Copy a single file using NIO.
     * @throws IOException
     */
    private static void nioCopy(FileOutputStream fos, FileInputStream fis) throws IOException {
        FileChannel outChannel = fos.getChannel();
        FileChannel inChannel = fis.getChannel();
        long length = inChannel.size();
        long offset = 0;
        while (true) {
            long remaining = length - offset;

            long toTransfer = remaining < MAX_TRANSFER_SIZE ? remaining : MAX_TRANSFER_SIZE;
            long transferredBytes = inChannel.transferTo(offset, toTransfer, outChannel);
            offset += transferredBytes;
            length = inChannel.size();
            if (offset >= length) {
                break;
            }
        }
    }

    /**
     * Copy a single file using the java.io.
     * @throws IOException
     */
    private static void oioCopy(File source, FileOutputStream fos, FileInputStream fis) throws IOException {
        int size = (int) (source.length() < MAX_TRANSFER_SIZE ? source.length() : MAX_TRANSFER_SIZE);
        byte[] buffer = new byte[size];
        int read;
        while ((read = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, read);
        }

    }
}

Related

  1. copy(File inputFile, OutputStream out)
  2. copy(File source, File dest)
  3. copy(File source, File dest)
  4. copy(File source, File dest)
  5. copy(File source, File dest)
  6. copy(File source, File dest, boolean preserveTime)
  7. copy(File source, File destination)
  8. copy(File source, File destination)
  9. copy(File source, File destination)