Here you can find the source of copyFile(File source, File target, boolean createParents, FileFilter filter)
Copies source
to target
.
public static void copyFile(File source, File target, boolean createParents, FileFilter filter) throws IOException
//package com.java2s; /*// w ww .j a va2s. co m * The MIT License (MIT) * * Copyright (c) 2010 Technische Universitaet Berlin * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileFilter; import java.util.List; import java.util.ArrayList; public class Main { /** * <p> * Copies <code>source</code> to <code>target</code>. If <code>target</code> * is a directory, <code>source</code> is copied to a new file inside that directory; * otherwise, <code>target</code> becomes the copy of <code>source</code>. If * <code>createParents</code> is <code>true</code>, parent directories of * <code>target</code> are created if necessary. * </p> * <p> * If <code>source</code> is a directory, it is copied recursively. In that case, The * files to be copied can be restricted by <code>filter</code>. If <code>filter</code> * is null, all files are copied. Please note: If a directory is excluded by * <code>filter</code>, all its descendent files are not copied, either (this is * slightly different to the behaviour of the {@link #listFiles IOUtil.listFiles} * method). * </p> */ public static void copyFile(File source, File target, boolean createParents, FileFilter filter) throws IOException { // At source name to target if we are copying to a directory: if (target.isDirectory()) { String path = target.getPath(); String name = source.getName(); target = new File(path.endsWith(File.separator) ? path + name : path + File.separator + name); } // Checking parents of target file: File parent = target.getParentFile(); if (parent != null && !parent.exists()) { if (createParents) createDir(parent, false); else throw new IOException("copyFile: Parent directory does not exist: " + target); } if (source.isDirectory()) { createDir(target, false); for (File file : source.listFiles(filter)) copyFile(file, target, createParents, filter); } else { // Input stream for the source: FileInputStream in = new FileInputStream(source); // Output stream for the target: FileOutputStream out = new FileOutputStream(target); // Copying: byte[] ioBuffer = new byte[1024]; int number = -1; while ((number = in.read(ioBuffer)) != -1) out.write(ioBuffer, 0, number); out.flush(); // Cleanup: in.close(); out.close(); } } /** * <p> * Copies <code>source</code> to <code>target</code>. If <code>target</code> * is a directory, <code>source</code> is copied to a new file inside that directory; * otherwise, <code>target</code> becomes the copy of <code>source</code>. If * <code>createParents</code> is <code>true</code>, parent directories of * <code>target</code> are created if necessary. If <code>source</code> is a directory, * it is copied recursively. * </p> */ public static void copyFile(File source, File target, boolean createParents) throws IOException { copyFile(source, target, createParents, null); } /** * <p> * Copies <code>source</code> to <code>destination</code>. If <code>destination</code> * is a directory, <code>source</code> is copied to a new file inside that directory; * otherwise, <code>destination</code> becomes the copy of <code>source</code>. * </p> * <p> * Same as {@link #copyFile(File,File,boolean) copyFile(source, destination, false)}. * </p> */ public static void copyFile(File source, File destination) throws IOException { copyFile(source, destination, false); } /** * <p> * Copies file <code>sourceName</code> (the source) to <code>destinationName</code>. If * the latter names a directory, the source is copied to a new file with the same local * name inside that directory; otherwise is is copied to a new file named * <code>destinationName</code>. * </p> * <p> * If <code>createParents</code> is <code>true</code>, parent directories of * <code>destination</code> are created if necessary. * </p> * * @param sourceName name of the source file * @param destinationName name of the destination file * @param createParents whether parent directories should be created if necessary */ public static void copyFile(String sourceName, String destinationName, boolean createParents) throws IOException { copyFile(new File(sourceName), new File(destinationName), createParents); } /** * <p> * Copies file <code>sourceName</code> (the source) to <code>destinationName</code>. If * the latter names a directory, the source is copied to a new file with the same local * name inside that directory; otherwise is is copied to a new file named * <code>destinationName</code>. * </p> * <p> * Same as *{@link #copyFile(String,String,boolean) copyFile(sourceName, destinationName, false)}. * </p> */ public static void copyFile(String sourceName, String destinationName) throws IOException { copyFile(sourceName, destinationName, false); } /** * Creates the specified directory. Necessary subdirectories are created, too, if not * existing. If <code>failIfExists</code> is true, an exception is thrown if the directory * already exists. If <code>failIfExists</code> is false, the method simply returns if the * directory already exists. If a file with the same name as the directory exists, but is * not a directory, an exception is thrown in any case. Unlike {@link File#mkdirs mkdirs} * in class {@link File File}, this method throws an exception if the creation failed, * instead of returning false. * * @throws IOException if the creation of the directory failed, <code>failIfExists</code> * is true and the directory already exists, or a non-directory file with the same name * exists. */ public static void createDir(File dir, boolean failIfExists) throws IOException { if (dir.exists()) { if (failIfExists) throw new IOException("Can not create directory: Directory already exists: " + dir); if (!dir.isDirectory()) throw new IOException("Can not create directory: File exists, but is not a directory: " + dir); } else { if (!dir.mkdirs()) throw new IOException("Failed to create directory: " + dir); } } /** * <p> * Creates the specified directory. Necessary subdirectories are created, too, if not * existing. If <code>failIfExists</code> is true, an exception is thrown if the directory * already exists. If <code>failIfExists</code> is false, the method simply returns if the * directory already exists. If a file with the same name as the directory exists, but is * not a directory, an exception is thrown in any case. Unlike {@link File#mkdirs mkdirs} * in class {@link File File}, this method throws an exception if the creation failed, * instead of returning false. * </p> * <p> * Same as {@link #createDir(File,boolean) createDir(new File(dirName), failIfExists)}. * </p> * * @throws IOException if the creation of the directory failed, <code>failIfExists</code> * is true and the directory already exists, or a non-directory file with the same name * exists. */ public static void createDir(String dirName, boolean failIfExists) throws IOException { createDir(new File(dirName), failIfExists); } /** * Returns all files in the specified directory accepted by the specified filter. If * <code>recursive</code> is true, subdirectories are processed recursivly; i.e., files in * descendant directories are returned, too. */ public static File[] listFiles(File dir, FileFilter filter, boolean recursive) { List<File> files = new ArrayList<File>(); scanFiles(dir, filter, recursive, files); return files.toArray(new File[files.size()]); } /** * Auxiliary method to implement {@link #listFiles listFiles}. */ protected static void scanFiles(File dir, FileFilter filter, boolean recursive, List<File> files) { if (!dir.isDirectory()) throw new IllegalArgumentException("scanFiles: not a directory: " + dir); for (File file : dir.listFiles()) { if (filter == null || filter.accept(file)) files.add(file); if (recursive && file.isDirectory()) scanFiles(file, filter, recursive, files); } } }