package homeWork;
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.util.Scanner;
public class HomeWork2 {
/**
* 3,从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入需要拷贝的文件夹");
String outPath = sc.nextLine();
System.out.println("请输入需要放在的文件夹");
String inPath = sc.nextLine();
copyDictory(outPath,inPath);//先复制文件夹
copyFile(outPath,inPath); //再复制文件
}
public static void copyDictory(String outPath, String inPath) throws IOException {
File out = new File(outPath);
// File in = new File(inPath);
String[] str = out.list();
for (int i = 0; i < str.length; i++) {
String outName = outPath + "\\" + str[i];
String intName = inPath + "\\" + str[i];
if (new File(outName).isDirectory()) {
new File(intName).mkdir();
copyDictory(outName,intName);
}
}
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream(outPath));
}
public static void copyFile(String outPath, String inPath) throws IOException{
File out = new File(outPath);
String[] str = out.list();
for (int i = 0; i < str.length; i++) {
String outName = outPath + "\\" + str[i];
String intName = inPath + "\\" + str[i];
if (new File(outName).isDirectory()) {
copyFile(outName,intName);
}else{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(outName));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(intName));
int len;
while((len = bis.read()) != -1){
bos.write(len);
}
bis.close();
bos.close();
}
}
}
}
|
|