您的位置:首页 > 编程语言 > Java开发

java 复制文件夹下所有文件到指定的文件夹,不改变目录结构

2013-07-29 01:39 901 查看
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Demo3 {
public static void main(String[] args) throws IOException {
String path = "/home/mrzhan/workspace/copy";
String copyPath = "/home/mrzhan/workspace/copyPath";
copy(path,copyPath);
}

public static void copy(String path, String copyPath) throws IOException{
File filePath = new File(path);
DataInputStream read ;
DataOutputStream write;
if(filePath.isDirectory()){
File[] list = filePath.listFiles();
for(int i=0; i<list.length; i++){
String newPath = path + File.separator + list[i].getName();
String newCopyPath = copyPath + File.separator + list[i].getName();
File newFile = new File(copyPath);
if(!newFile.exists()){
newFile.mkdir();
}
copy(newPath, newCopyPath);
}
}else if(filePath.isFile()){
read = new DataInputStream(
new BufferedInputStream(new FileInputStream(path)));
write = new DataOutputStream(
new BufferedOutputStream(new FileOutputStream(copyPath)));
byte [] buf = new byte[1024*512];
while(read.read(buf) != -1){
write.write(buf);
}
read.close();
write.close();
}else{
System.out.println("请输入正确的文件名或路径名");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐