栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

将指定目录下文件通过筛选复制到别的目录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将指定目录下文件通过筛选复制到别的目录

自己写的简要的工具类,简单的可以直接拿去用着玩,用上了记得点赞哈= - =

package IO.ControllerFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;


public class Test {
    

    public static void main(String[] args) throws IOException {
        String path = "C:\Users\W\Desktop\java 学习项目";
        String to_path = "C:\Users\W\Desktop\ss";
        
        String[] suffix = new String[]{"png", "jpg", "gif"};
        get_DirFile_To_Dir(path, to_path, suffix, null,0);
    }

    
    public static void get_DirFile_To_Dir(String path, String to_path, String[] suffix, String[] prefix, int prefix_start) {
        File path_file = new File(path);
        if (!path_file.exists()) {
            throw new RuntimeException("找不到指定路径下的文件或文件夹--查找源必须存在" + path);
        }
        File to_path_file = new File(to_path);
        if (!to_path_file.exists()) {
            if (to_path_file.mkdirs()){
                System.out.println("无目标目录--创建目录成功");
            }else {
                throw new RuntimeException("创建目录失败--" + to_path);
            }
        }
        File[] allFile = getAllFile(path,suffix,prefix,prefix_start);
        Set files = new HashSet();
        for (File file : allFile) {
            if (file != null) {
                files.add(file);
            }
        }
        for (Object file : files) {
            File temp = (File) file;
            try {
                copy(temp.getAbsolutePath(), to_path);
            } catch (IOException e) {
                System.out.println(temp.getName() + "传输失败");
                e.printStackTrace();
            }
            System.out.println(temp.getName());
        }
    }

    //获取文件夹下所有文件
    public static File[] getAllFile(String path, String[] suffix, String[] prefix, int prefix_start) {
        
        File file = new File(path);
        String[] file_list = file.list();
        // 初始化长度
        File[] allFile = new File[file_list.length];

        
        Boolean flag = true;
        int count ;

        //遍历长度
        for (int i = 0; i < file_list.length; i++) {
            File f = new File(path + "\" + file_list[i]);
            if (f.isDirectory()) {
                File[] files = getAllFile(f.getAbsolutePath(), suffix, prefix,0);
                // 按照大小扩容
                allFile = Arrays.copyOf(allFile, allFile.length + files.length);
                //将目标数据放入新数组 这种是修改了传入的数组,一般都java不能修改传入数组,所以是本地方法
                System.arraycopy(files, 0, allFile, allFile.length - files.length, files.length);
            } else {
                
                String name = f.getName();
                if (suffix != null && suffix.length > 0) {
                    count = 0;
                    for (String s : suffix) {
                        if (name.endsWith(s)) {
                           count++;
                        }
                    }
                    if (count == 0) {
                        flag = false;
                    }
                }
                if (prefix != null && prefix.length > 0) {
                    count = 0;
                    for (String s : prefix ) {
                        if (name.startsWith(s,prefix_start)) {
                            count++;
                        }
                    }
                    if (count == 0) {
                        flag = false;
                    }
                }
                if (flag) {
                    allFile = Arrays.copyOf(allFile, allFile.length + 1);
                    allFile[allFile.length - 1] = f;
                }
            }
        }
        return allFile;
    }


    
    public static void copy(String src, String dest) throws IOException {
        File file = new File(src);
        if (!file.exists()) {
            //可以起到一个抛出异常中断程序的效果
            throw new RuntimeException("找不到指定路径下的文件或文件夹" + src);
        }
        //初始化I/O流
        InputStream in = null;
        OutputStream out = null;
        //如果文件夹是目录,则在指定路径下新建目录,如果是文件则直接复制
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            String cur = dest + File.separatorChar + file.getName();
            File file1 = new File(cur);
            if (!file1.mkdir()) {
                throw new RuntimeException("非法路径" + dest);
            }
            for (File file2 : files) {
                copy(file2.getAbsolutePath(), file1.getAbsolutePath());
            }
        } else {
            in = new FileInputStream(file);
            out = new FileOutputStream(dest + File.separatorChar + file.getName());
            //缓存数组
            byte[] a = new byte[1024];
            int len = -1;
            while ((len = in.read(a)) != -1) {
                out.write(a);
            }
            //将缓存写入目标文件
            out.flush();
            //先创建先释放,后创建后释放
            out.close();
            in.close();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830565.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号