package com.jd.MoLiyingDemo;


 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 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 FileCopyDemo {


/**
* 复制整个目录到指定目录
*/
public static void main(String[] args) {
File oldpath=new File("C:\\ASworkspace\\");
File newpath=new File("D:\\java_copy\\");
copy(oldpath, newpath);
System.out.println("文件夹复制成功");
}

/**
* @param oldpath 要复制的文件夹
* @param newpath 复制到新的路径
*/
public static void copy(File oldpath,File newpath){
if(oldpath.isDirectory()){//复制文件夹
newpath.mkdir();
File[] oldList = oldpath.listFiles();
if(oldList!=null){
for (File file : oldList) {
copy(file, new File(newpath,file.getName()));
}
}
}else if(oldpath.isFile()){//复制文件
File f=new File(newpath.getAbsolutePath());
try {
f.createNewFile();
copyFile(oldpath,f.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* @param filepath 要复制的文件
* @param path  复制到哪去
*/
public static void copyFile(File filepath,String path){
try {
InputStream is=new FileInputStream(filepath);
OutputStream os=new FileOutputStream(path);

BufferedInputStream bis=new BufferedInputStream(is);
BufferedOutputStream bos=new BufferedOutputStream(os);


byte[] bs=new byte[1024];
int len=-1;
while((len=bis.read(bs))!=-1){
bos.write(bs, 0, len);
}
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}


 }