import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class FileTest {/** * @param args */public static void main(String[] args) {String strFileSrc = "D://demo";String strFileDes = "D://demotest";File fileSrc = new File(strFileSrc);File fileDes = new File(strFileDes);FileTest obj = new FileTest(); System.out.println("开始"); obj.copyFolder(fileSrc, fileDes); System.out.println("结束");}private void copyFolder(File src, File dest){if (src.isDirectory()) {if (!dest.exists()) {dest.mkdir();}String files[] = src.list();for (String file : files) {File srcFile = new File(src, file);File destFile = new File(dest, file);// 递归复制copyFolder(srcFile, destFile);}} else {try {InputStream in = new FileInputStream(src);OutputStream out = new FileOutputStream(dest);byte[] buffer = new byte[1024];int length;while ((length = in.read(buffer)) > 0) {out.write(buffer, 0, length);}in.close();out.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch(IOException e2){e2.printStackTrace();}}}}
取消
评论