Copy the code java folder achieve

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyTest2 {
    public static void main(String[] args) {
        copyDir("D:\\Documents\\Downloads\\jieya", "D:\\");
    }

    public static void copyDir(String fileAdress, String copyString) {
        File file = new File(fileAdress);
        if (file.isFile()) {
            try {
                copyFile(file.getAbsolutePath(), copyString+"\\"+file.getName());
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            File file1 = new File(copyString+"\\"+file.getName());
            file1.mkdirs();
            String[] list = file.list();
            for (int i = 0; i < list.length; i++) {
                copyDir(file.getAbsolutePath() + "\\" + list[i], file1.getAbsolutePath() );
            }
        }
    }
    //复制方法文件输入流
    public static void copyFile(String oldString, String copyString) throws IOException {
        FileInputStream in = new FileInputStream(oldString);
        FileOutputStream out = new FileOutputStream(copyString);
        int len = 0;
        byte[] bytes = new byte[1024 * 8];
        while ((len = in.read(bytes)) != -1) {
            out.write(len);
        }
        in.close();
        out.close();
    }
}
Published 15 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/Junzizhiai/article/details/102838367