Java File Copy nio copy(File source, File target, FilenameFilter filter)

Here you can find the source of copy(File source, File target, FilenameFilter filter)

Description

Copy file or directory to the specified destination.

License

Open Source License

Parameter

Parameter Description
source copy source
target copy destination
filter copy filter

Exception

Parameter Description

Declaration

public static void copy(File source, File target, FilenameFilter filter) 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.nio.channels.FileChannel;

import java.util.LinkedList;

public class Main {
    /** Represents filter what select any file */
    public static final FilenameFilter ANY_FILTER = new FilenameFilter() {
        @Override//from www .j av a  2 s . c  o  m
        public boolean accept(File dir, String name) {
            return true;
        }
    };

    /**
     * Copy file or directory to the specified destination. Existed files in destination directory will be overwritten.
     *
     * @param source
     *         copy source
     * @param target
     *         copy destination
     * @param filter
     *         copy filter
     * @throws java.io.IOException
     *         if any i/o error occurs
     */
    public static void copy(File source, File target, FilenameFilter filter) throws IOException {
        copy(source, target, filter, false, true);
    }

    /**
     * Copy file or directory to the specified destination.
     *
     * @param source
     *         copy source
     * @param target
     *         copy destination
     * @param filter
     *         copy filter
     * @param replaceIfExists
     *         if <code>true</code>  existed files in destination directory will be overwritten
     * @throws java.io.IOException
     *         if any i/o error occurs
     */
    public static void copy(File source, File target, FilenameFilter filter, boolean replaceIfExists)
            throws IOException {
        copy(source, target, filter, false, replaceIfExists);
    }

    private static void copy(File source, File target, FilenameFilter filter, boolean nio, boolean replaceIfExists)
            throws IOException {
        if (source.isDirectory()) {
            if (!(target.exists() || target.mkdirs())) {
                throw new IOException(String.format("Unable create directory '%s'. ", target.getAbsolutePath()));
            }
            if (filter == null) {
                filter = ANY_FILTER;
            }
            String sourceRoot = source.getAbsolutePath();
            LinkedList<File> q = new LinkedList<>();
            q.add(source);
            while (!q.isEmpty()) {
                File current = q.pop();
                File[] list = current.listFiles();
                if (list != null) {
                    for (File f : list) {
                        if (!filter.accept(current, f.getName())) {
                            continue;
                        }
                        File newFile = new File(target, f.getAbsolutePath().substring(sourceRoot.length() + 1));
                        if (f.isDirectory()) {
                            if (!(newFile.exists() || newFile.mkdirs())) {
                                throw new IOException(
                                        String.format("Unable create directory '%s'. ", newFile.getAbsolutePath()));
                            }
                            if (!f.equals(target)) {
                                q.push(f);
                            }
                        } else {
                            if (nio) {
                                nioCopyFile(f, newFile, replaceIfExists);
                            } else {
                                copyFile(f, newFile, replaceIfExists);
                            }
                        }
                    }
                }
            }
        } else {
            File parent = target.getParentFile();
            if (!(parent.exists() || parent.mkdirs())) {
                throw new IOException(String.format("Unable create directory '%s'. ", parent.getAbsolutePath()));
            }
            if (nio) {
                nioCopyFile(source, target, replaceIfExists);
            } else {
                copyFile(source, target, replaceIfExists);
            }
        }
    }

    private static void nioCopyFile(File source, File target, boolean replaceIfExists) throws IOException {
        if (!target.createNewFile()) // atomic
        {
            if (target.exists() && !replaceIfExists) {
                throw new IOException(String.format("File '%s' already exists. ", target.getAbsolutePath()));
            }
        }
        FileInputStream sourceStream = null;
        FileOutputStream targetStream = null;
        FileChannel sourceChannel = null;
        FileChannel targetChannel = null;
        try {
            sourceStream = new FileInputStream(source);
            targetStream = new FileOutputStream(target);
            sourceChannel = sourceStream.getChannel();
            targetChannel = targetStream.getChannel();
            final long size = sourceChannel.size();
            long transferred = 0L;
            while (transferred < size) {
                transferred += targetChannel.transferFrom(sourceChannel, transferred, (size - transferred));
            }
        } finally {
            if (sourceChannel != null) {
                sourceChannel.close();
            }
            if (targetChannel != null) {
                targetChannel.close();
            }
            if (sourceStream != null) {
                sourceStream.close();
            }
            if (targetStream != null) {
                targetStream.close();
            }
        }
    }

    private static void copyFile(File source, File target, boolean replaceIfExists) throws IOException {
        if (!target.createNewFile()) {
            if (target.exists() && !replaceIfExists) {
                throw new IOException(String.format("File '%s' already exists. ", target.getAbsolutePath()));
            }
        }
        FileInputStream in = null;
        FileOutputStream out = null;
        byte[] b = new byte[8192];
        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(target);
            int r;
            while ((r = in.read(b)) != -1) {
                out.write(b, 0, r);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. copy(File source, File destination)
  2. copy(File source, File destination)
  3. copy(File source, File target)
  4. copy(File source, File target)
  5. copy(File source, File target)
  6. copy(File sourceFile, File destFile)
  7. copy(File sourceFile, File destFile)
  8. copy(File sourceFile, File destinationFile)
  9. copy(File sourceFile, File targetFile)