文件操作工具类FileUtils

public class FileUtils {
    public static void main(String[] args) {
        List<File> lists = getAllFiles("c:/");
        for (File file : lists) {
            System.out.println(file.getName());
        }
    }
    /**
     * 获取指定目录下的所有文件
     */
    public static List<File> getAllFiles(String dir) {
        return getAllFiles(new File(dir), null);
    }

    public static List<File> getAllFiles(File file) {
        return getAllFiles(file, null);
    }

    public static List<File> getAllFiles(String dir, String[] suffuxs) {
        return getAllFiles(new File(dir), suffuxs);
    }

    /**
     * 获取指定目录下的符合后缀规则的所有文件
     * 
     * @param file
     *            源文件夹
     * @param suffuxs
     *            后缀规则
     * @return
     */
    public static List<File> getAllFiles(File file, String[] suffuxs) {
        List<File> list = new ArrayList<File>();
        if (file.isDirectory()) {
            // 递归
            cursionDir(list, file, suffuxs);
        } else {
            throw new IllegalArgumentException("这是一个文件,请输入文件夹");
        }
        return list;
    }

    private static void cursionDir(List<File> list, File file, String[] suffuxs) {
        File[] files = file.listFiles(new FileFilter() {
            // 过滤器
            @Override
            public boolean accept(File pathname) {
                if (suffuxs == null) {
                    return true;
                }
                if (pathname.isDirectory()) {
                    return true;
                }
                for (String string : suffuxs) {
                    if (pathname.getAbsolutePath().endsWith(string)) {
                        return true;
                    }
                }
                return false;
            }
        });
        if (files == null) {
            return;
        } else {
            for (File file2 : files) {
                if (file2.isDirectory()) {
                    cursionDir(list, file2, suffuxs);
                } else if (file2.isFile()) {
                    list.add(file2);
                }
            }
        }
    }

    /**
     * 复制文件
     * 
     * @param src
     *            源文件路径
     * @param dest
     *            目标文件路径
     */
    public static boolean copyFile(String src, String dest) {
        return copyFile(new File(src), new File(dest));
    }

    public static boolean copyFile(File filesrc, File filedest) {
        // File filesrc = new File(src);
        // File filedest = new File(dest);
        filedest.getParentFile().mkdirs();
        InputStream fis = null;
        OutputStream fos = null;
        try {
            fis = new FileInputStream(filesrc);
            fos = new FileOutputStream(filedest);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = fis.read(buf)) != -1) {
                fos.write(buf, 0, len);
                fos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 复制文件夹
     * 
     * @param src
     *            源文件夹路径
     * @param dest
     *            目的文件夹路径
     * @return
     */
    public static boolean copyDir(String src, String dest) {
        File filesrc = new File(src);
        File filedest = new File(dest);
        if (filesrc.isFile()) {
            throw new IllegalArgumentException("这是一个文件,请调用copyFile方法");
        } else if (!filesrc.exists()) {
            throw new IllegalArgumentException("源文件夹不存在");
        }
        filedest.mkdirs();
        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(2);
        List<File> lists = getAllFiles(filesrc);
        for (File file : lists) {
            if (file.isFile()) {
                String fileName = file.getAbsolutePath().replace(
                        filesrc.getAbsolutePath(), filedest.getAbsolutePath());
                // copyFile(src, new File(fileName));
                newFixedThreadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        copyFile(file, new File(fileName));
                    }
                });
                System.out.println(Thread.currentThread().getName() + "复制文件"
                        + file.getAbsolutePath() + "到" + fileName);
            }
        }
        newFixedThreadPool.shutdown();
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值