- package practice;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class Test2 {
- public static void main(String[] args) throws IOException {
- File f1 = new File("F:\\111");
- File f2 = new File("E:\\111");
- if (!f2.exists()) {
- f2.mkdir();
- }
- copyfile(f1, f2);
- }
- private static void copyfile(File f1, File f2) throws IOException {
- File[] files = f1.listFiles();
- for (File f : files) {
- String name = f.getName();
- if (f.isDirectory()) {
- File f3 = new File(f2 + "\\" + name);
- f3.mkdir();
- copyfile(f, f3);
- } else {
- File f4 = new File(f2, name);
- copy(f, f4);
- }
- }
- }
- private static void copy(File f, File f4) throws IOException {
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f4));
- int len = 0;
- byte[] bytes = new byte[1024];
- while ((len = bis.read(bytes)) != -1) {
- bos.write(bytes, 0, len);
- }
- bos.close();
- bis.close();
- }
- }
复制代码 |