其他分享
首页 > 其他分享> > 复制文件夹

复制文件夹

作者:互联网

单级文件夹

package myCopyFolderDemo;

import java.io.*;

public class CopyFolderDemo {
    public static void main(String[] args) throws IOException {
        //创建数据源目录File对象,路径是F:\\itcast
        File srcFolder = new File("F:\\itcast");

        //获取数据源目录File对象的名称
        String srcFolderName = srcFolder.getName();

        //创建目的地目录File对象,路径名是模块名+itcast组成
        File destFolder = new File("Test", srcFolderName);

        //判断目的地目录对应的File是否存在,如果不存在,就创建
        if (!destFolder.exists()) {
            destFolder.mkdir();
        }

        //获取数据源目录下所有文件的File数组
        File[] listFiles = srcFolder.listFiles();

        //遍历File数组,得到每一个File对象,该File对象,其实就是数据源文件
        for (File srcFile : listFiles) {
            //获取数据源文件File对象的名称
            String srcFileName = srcFile.getName();
            //创建目的地文件File对象,路径名是目的地目录+mn.jpg组成
            File destFile = new File(destFolder, srcFileName);
            //复制文件
            copyFile(srcFile, destFile);
        }
    }

    private static void copyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        bos.close();
        bis.close();
    }
}

多级文件夹

package myCopyFolderDemo;

import java.io.*;

public class CopyFoldersDemo {
    public static void main(String[] args) throws IOException {
        //创建数据源File对象,路径是F:\\itcast
        File srcFile = new File("F:\\itcast");

        //创建目的地File对象,路径是F:\\itcast2
        File destFile = new File("F:\\itcast2");

        //写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
        copyFolder(srcFile, destFile);
    }

    private static void copyFolder(File srcFile, File destFile) throws IOException {
    	//判断数据源File是否是目录
        if (srcFile.isDirectory()) {
            String srcFileName = srcFile.getName();
            File newFolder = new File(destFile, srcFileName);
            if (!newFolder.exists()) {
                newFolder.mkdir();
            }

            File[] fileArray = srcFile.listFiles();

            for (File file : fileArray) {
                copyFolder(file, newFolder);
            }
        }
        else {
            File newFile = new File(destFile, srcFile.getName());
            copyFile(srcFile, newFile);
        }
    }

    private static void copyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        bos.close();
        bis.close();
    }
}

异常处理

package myCopyFolderDemo;

import java.io.*;

public class CopyFileExceptionDemo {
    public static void main(String[] args) {

    }

    private static void copyFile2(File srcFile, File destFile) {
        FileReader fr = null;
        FileWriter fw = null;
        try{
            fr = new FileReader("fr.txt");
            fw = new FileWriter("fr.txt");

            char[] chs = new char[1024];
            int len;
            while ((len = fr.read()) != -1) {
                fw.write(chs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //JDK7的改进方案
    private static void copyFile3(File srcFile, File destFile) {
        try(FileReader fr = new FileReader("fr.txt");
            FileWriter fw = new FileWriter("fr.txt");){
            char[] chs = new char[1024];
            int len;
            while ((len = fr.read()) != -1) {
                fw.write(chs, 0, len);
            }
            fw.close();
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:fr,srcFile,复制,destFile,文件夹,len,File,new
来源: https://blog.csdn.net/he0707/article/details/117338956