Here you can find the source of copyDirectoryContents(File source, File destination)
Parameter | Description |
---|---|
source | the source directory |
destination | the destination directory |
Parameter | Description |
---|---|
IOException | if there are IO errors |
public static void copyDirectoryContents(File source, File destination) throws IOException
//package com.java2s; /*==========================================================================*\ | $Id: FileUtilities.java,v 1.5 2014/06/16 15:59:40 stedwar2 Exp $ |*-------------------------------------------------------------------------*| | Copyright (C) 2006-2012 Virginia Tech | | This file is part of Web-CAT.//from w w w . j a va2s . c om | | Web-CAT is free software; you can redistribute it and/or modify | it under the terms of the GNU Affero General Public License as published | by the Free Software Foundation; either version 3 of the License, or | (at your option) any later version. | | Web-CAT is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU Affero General Public License | along with Web-CAT; if not, see <http://www.gnu.org/licenses/>. \*==========================================================================*/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Recursively copies the contents of the source directory into the * destination directory. * * @param source the source directory * @param destination the destination directory * @throws IOException if there are IO errors */ public static void copyDirectoryContents(File source, File destination) throws IOException { File[] fileList = source.listFiles(); if (fileList != null) { for (int i = 0; i < fileList.length; i++) { if (fileList[i].isDirectory()) { // Copy the whole directory File newDir = new File(destination, fileList[i].getName()); newDir.mkdir(); copyDirectoryContents(fileList[i], newDir); } else if (fileList[i].getName().equals(".DS_Store")) { // ignore these for Mac OSX } else { copyFileToDir(fileList[i], destination); } } } } /** * Copies a file into a specified directory * * @param oldFile the file to copy * @param outputDir the destination directory * @throws IOException if there are IO errors */ public static void copyFileToDir(File oldFile, File outputDir) throws IOException { FileInputStream in = new FileInputStream(oldFile); File destFile = new File(outputDir, oldFile.getName()); FileOutputStream out = new FileOutputStream(destFile); copyStream(in, out); in.close(); out.close(); destFile.setLastModified(oldFile.lastModified()); } /** * Copies the contents of an input stream onto an existing output * stream. The output stream is flushed when the operation * is complete. * * @param in the input stream to copy * @param out the destination * @throws IOException if there are IO errors */ public static void copyStream(InputStream in, OutputStream out) throws IOException { final int BUFFER_SIZE = 65536; // read in increments of BUFFER_SIZE byte[] b = new byte[BUFFER_SIZE]; int count = in.read(b); while (count > -1) { out.write(b, 0, count); count = in.read(b); } out.flush(); } }