利用java IO流处理 文件复制问题
1.[代码][Java]代码
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
String newPath = path + File.separator + list[i].getName();
String newCopyPath = copyPath + File.separator + list[i].getName();
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("请输入正确的文件名或路径名");
}
}
}