Java File Copy nio copyFile(File source, File dest, boolean visibleFilesOnly)

Here you can find the source of copyFile(File source, File dest, boolean visibleFilesOnly)

Description

Copy a file from source to dest.

License

BSD License

Parameter

Parameter Description
source a parameter
dest a parameter
visibleFilesOnly a parameter

Exception

Parameter Description
IOException an exception

Declaration

private static void copyFile(File source, File dest, boolean visibleFilesOnly) throws IOException 

Method Source Code

//package com.java2s;
/**/*w  ww  . j  a v  a2  s. c om*/
 * <p>
 * Utilities for manipulating Paths, Files, Directories, etc.
 * </p>
 * <p>
 * <span class="BSDLicense"> This software is distributed under the <a
 * href="http://hci.stanford.edu/research/copyright.txt">BSD License</a>.</span>
 * </p>
 * 
 * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu)
 */

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copy a file from source to dest.
     * 
     * @param source
     * @param dest
     * @param visibleFilesOnly
     * @throws IOException
     */
    private static void copyFile(File source, File dest, boolean visibleFilesOnly) throws IOException {
        if (visibleFilesOnly && isHiddenOrDotFile(source)) {
            // this source file does not fit the flag
            return;
        }

        if (dest.exists()) {
            System.err.println("Destination File Already Exists: " + dest);
        }

        FileChannel in = null, out = null;
        try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(dest).getChannel();
            in.transferTo(0, in.size(), out);

            // an alternate way...
            // long size = in.size();
            // final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
            // out.write(buf);

        } finally {
            if (in != null) {
                in.close();
                // System.out.println("Closed In");
            }
            if (out != null) {
                out.close();
                // System.out.println("Closed Out");
            }
        }
    }

    /**
     * @param possiblyHiddenFile
     * @return if the file is hidden (either hidden flag, or name starts with a dot)
     */
    public static boolean isHiddenOrDotFile(final File possiblyHiddenFile) {
        return possiblyHiddenFile.isHidden() || possiblyHiddenFile.getName().startsWith(".");
    }
}

Related

  1. copy(File srcfile, File destfile)
  2. copy(File srcFile, File dstFile)
  3. copy(File srcFileOrDir, File targetFileOrDir)
  4. copy(File toFile, File fromFile)
  5. copyFile(File f1, File f2)
  6. copyFile(File src, File dest)
  7. copyFile(FileChannel srcFc, File dstFile)
  8. copyFile(final File source, final File dest)
  9. copyFile(final File source, final File target)