Here you can find the source of copyFile(File source, File dest, boolean visibleFilesOnly)
Parameter | Description |
---|---|
source | a parameter |
dest | a parameter |
visibleFilesOnly | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
private static void copyFile(File source, File dest, boolean visibleFilesOnly) throws IOException
//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("."); } }