package two;
import java.io.*;
public class Test11 {
public static void main(String[] args) {
File file = new File("D:\\info456");
file.mkdir();
copyFiles();
}
public static void copyFiles() {
File file = new File("D:\\info123");
String[] list = file.list();
for (String name : list)
copy(new File(file, name), name);
}
//copy()方法传入两个参数,绝对路径和子目录名,子目录名是append的
public static void copy(File f, String d) {
File root = new File("D:\\info456");
if (f.isDirectory()) {
File dd = new File(root,d);
dd.mkdir();
for (String sub : f.list()) {
File ff = new File(f, "\\" + sub);
copy(ff, d + "\\" + sub);
}
}
if (f.isFile()) {
File file = new File(root, d);
//System.out.println(f.getPath() + "是文件,复制到"+root.getPath()+"\\"+d);
try {
file.createNewFile();
//System.out.println("----------" + file.getAbsoluteFile());
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(file);
if (fis.available() != 0) fos.write(fis.readAllBytes());
fos.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
System.err.println(f.getAbsolutePath()+"复制到"+file.getAbsolutePath()+"出现问题");
}
}
}
}
06-24
501