package fileoutstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo03 {
public static void main(String[] args) throws IOException {
Demo04 demo04 = new Demo04();
File file1 = new File("F:\\IDEA\\code\\test");
File file2 = new File("F:\\IDEA\\code\\test1");
File input = new File("F:\\IDEA\\code\\test\\1.txt");
File output = new File("F:\\IDEA\\code\\test1");
demo04.copydir(file1,file2);
}
}
package fileoutstream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo04 {
public static void copydir(File file1,File file2) throws IOException {
if(!file1.exists()&&file1.isDirectory()){
throw new RuntimeException("问价夹必须存在");
}
File file = new File(file2, file1.getName());
file.mkdir();
File[] files = file1.listFiles();
if(files!=null){
for (File file3 : files) {
if(file3.isFile()){
copy(file3,file);
}else {
copydir(file3,file2);
}
}
}
}
public static void copy(File input, File output) throws IOException {
File dir = new File(output, input.getName());
dir.createNewFile();
FileInputStream fileInputStream = new FileInputStream(input);
FileOutputStream fileOutputStream = new FileOutputStream(dir);
byte[] bytes = new byte[4];
int len;
while ((len=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
fileInputStream.close();
fileOutputStream.close();
}
}