Here you can find the source of copyFiles(File file, File destPath)
Parameter | Description |
---|---|
file | a parameter |
destPath | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFiles(File file, File destPath) throws IOException
//package com.java2s; /*/*from w ww .jav a 2s . c o m*/ * Copyright (C) 2013 The Open Source Project By Yunying.Zhang * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * author:Yunying.Zhang * date:2013-09-27 */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static final int BUFFER_SIZE = 1024 * 1024 * 1; /** * Copy files to destPath, can be a folder. * @param file * @param destPath * @throws IOException */ public static void copyFiles(File file, File destPath) throws IOException { if (file.isDirectory()) { File dest = new File(destPath, file.getName()); File[] files = file.listFiles(); for (File subFile : files) { copyFiles(subFile, dest); } } else { copyFile(file, destPath); } } /** * Copy a file to destPath. only file. * @param file * @param destPath * @throws IOException */ public static void copyFile(File file, File destPath) throws IOException { if (file.isDirectory()) { return; } if (!destPath.exists()) { destPath.mkdirs(); } FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(new File(destPath, file.getName())); byte[] buf = new byte[BUFFER_SIZE]; int length = -1; while ((length = fis.read(buf)) != -1) { fos.write(buf, 0, length); } fos.flush(); fos.close(); fis.close(); } }