吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1656|回复: 10
收起左侧

[Java 原创] java 文件分类

[复制链接]
我是白小白 发表于 2023-1-14 21:35



首先 把 需要分类的 文件后缀 写上
配置分类后 保存到哪个文件夹



然后启动main方法即可

一个刚入行不久当得 程序员
程序如有bug 欢迎 各位大佬指正


就懒得打包exe 了  



配置



按照 文件 后缀 加 最后访问日期 分类



分类结果














[Java] 纯文本查看 复制代码
public class Test {

    private static  List<String> files = new ArrayList<String>();
    private static  List<String> ends = new ArrayList<String>();

    private static Integer count = 0;

    private static String path = "D:\\File\\分类\\";

    public static void main(String[] args) throws Exception {
        ends.add("pdf");
        ends.add("doc");
        ends.add("docx");
        ends.add("xls");
        ends.add("xlsx");
        ends.add("ppt");
        ends.add("pptx");
        ends.add("txt");



        ends.stream().forEach(e->{
            String dirStr = path+e.toString();
            File directory = new File(dirStr);
            if (!directory.exists()) {
                directory.mkdirs();
            }
        });


//        ends.add(".txt");
//        ends.add(".rar");
//        ends.add(".zip");
//        ends.add(".7z");
//        deleteChina("C:\\Users\\15400\\Documents");
//        deleteChina("D:\\");
        classifyFile();
    }


    public static void classifyFile(){
        classifyFile(null);
        FutureUtil.shutdown();
    }

    public static void classifyFile(String fileLocation) {
        count++;
        System.out.println(count);

            File file;
            if (!HuStringUtils.isEmpty(fileLocation)){
                file = new File(fileLocation);
            }else {
                file = null;
                File[] parts =File.listRoots();

                Arrays.stream(parts).forEach(e->{
                    ArrayList<String> dir = Dir(new File(e.toString()));
                    dir.stream().forEach(f->{
                        classifyFile(f);
                    });
                });
            }

            if (null!=file &&file.exists()) {

                if (file.isDirectory() && !path.equals(fileLocation)) {
                    System.out.print("--文件夹");
                    System.out.println(fileLocation);
                    ArrayList<String> dir = Dir(new File(fileLocation));

                    FutureUtil.synchronizeExecute(2, TimeUnit.HOURS,()->{
                        System.out.println("执行"+Thread.currentThread().getName());
                        dir.stream().forEach(e->{
                            classifyFile(e);
                        });
                        return null;
                    });

                } else {
                    System.out.print("--文件");
                    System.out.println(fileLocation);

                    ends.forEach(e->{
                        if (fileLocation.endsWith(e)){
                            file.getName();

                            String lastAccessTime = getLastAccessTime(file);

                            String pathName =path +e+"\\"+lastAccessTime.replaceAll("-","").substring(0,6)+"\\";

                            File pathFile = new File(pathName);
                            if (!pathFile.exists()) {
                                pathFile.mkdirs();
                            }


                            File copyFile = new File(pathName+ file.getName());

                            try {
                                copyFile(file, copyFile);
                                count++;
                            } catch (IOException ex) {
                                throw new RuntimeException(ex);
                            }
                            files.add(fileLocation);
                            System.out.println(Thread.currentThread().getName());
                        }
                    });


//                Stream<String> of1 = StreamUtil.of(file, CharsetUtil.CHARSET_UTF_8);
//                StringBuilder content = new StringBuilder();
//
//                of1.forEachOrdered(e->{
//                    content.append(e.replaceAll(REGEX_CHINESE,"")).append("\n");
//                });
//
//                BufferedWriter out = null;
//                try {
//                    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
//                    String target = content.toString();
//                    out.write(target);
//                    out.flush();
//                    out.close();
//                } catch (Exception e) {
//                }finally {
//                }
                }
            }

//            return null;



    }


    private static void copyFile(File source, File dest) throws IOException {
        if (!dest.exists()) {
            FileChannel inputChannel = null;
            FileChannel outputChannel = null;
            try {
                inputChannel = new FileInputStream(source).getChannel();
                outputChannel = new FileOutputStream(dest).getChannel();
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            } finally {
                inputChannel.close();
                outputChannel.close();
            }
        }
    }



    public static ArrayList<String> Dir(File dirFile)  {
        ArrayList<String> dirStrArr = new ArrayList<String>();

        if (dirFile.exists()) {
            //直接取出利用listFiles()把当前路径下的所有文件夹、文件存放到一个文件数组
            File files[] = dirFile.listFiles();
            if (null!=files ) {
                for (File file : files) {
                    //如果传递过来的参数dirFile是以文件分隔符,也就是/或者\结尾,则如此构造
                    if (dirFile.getPath().endsWith(File.separator)) {
                        dirStrArr.add(dirFile.getPath() + file.getName());
                    } else {
                        //否则,如果没有文件分隔符,则补上一个文件分隔符,再加上文件名,才是路径
                        dirStrArr.add(dirFile.getPath() + File.separator + file.getName());
                    }
                }
            }
        }
        return dirStrArr;
    }



    public static void readeFile(File file) throws IOException {
        if(file.exists()){
            for(File fileson : file.listFiles()){
                if(fileson.isFile()){
                    //对java文件逐行读取修改注释
                    BufferedReader reader = null;
                    try {
                        reader = new BufferedReader(new FileReader(fileson));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    StringBuilder content = new StringBuilder();
                    String tmp = null;
                    while ((tmp = reader.readLine()) != null) {
                        if(tmp.indexOf("//") >= 0){
                            tmp = tmp.replace(tmp.substring(tmp.indexOf("//"),tmp.length()),"");
                        }
                        //删除已空格开始,以空格结尾的行
                        if(tmp.matches("^\t+$")){
                            continue;
                        }

                        //删除空行
                        tmp = tmp.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1");
                        if("".equals(tmp) || null == tmp || "\t".equals(tmp) || "".equals(tmp.trim())){
                            continue;
                        }
                        content.append(tmp);
                        content.append("\n");
                    }
                    String target = content.toString();
                    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileson)));
                    out.write(target);
                    out.flush();
                    out.close();
                } else {
                    System.out.println(fileson.getName());
                    readeFile(fileson);
                }
            }
        }
    }





    public static String getLastAccessTime(File file) {
        if (file == null) {
            return null;
        }

        BasicFileAttributes attr = null;
        try {
            Path path =  file.toPath();
            attr = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 上次访问时间
        Instant instant = attr.lastAccessTime().toInstant();
        String format = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault()).format(instant);
        return format;
    }







}
图片.png

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
Bob5230 + 1 + 1 热心回复!
debug_cat + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 我是白小白 发表于 2023-1-14 21:39
[Java] 纯文本查看 复制代码
private static String REGEX_CHINESE = "[\u4e00-\u9fa5]";

注释代码打开  指定目录  关掉 copy文件的 就一键去 中文注释 了
82813668 发表于 2023-1-14 22:24
Chwmw 发表于 2023-1-14 22:31
yippee 发表于 2023-1-14 23:32
谢谢楼主分享
luxingyu329 发表于 2023-1-15 02:53
FutureUtil     与   HuStringUtils    是工具类吗?
 楼主| 我是白小白 发表于 2023-1-15 07:20
luxingyu329 发表于 2023-1-15 02:53
FutureUtil     与   HuStringUtils    是工具类吗?

啊 这我写的 。我上班代码也放上去
debug_cat 发表于 2023-1-15 11:14
感谢分析工具,先收藏,后面可能用得上
Bob5230 发表于 2023-1-16 20:00
很实用学习到了
yanjianye2018 发表于 2023-2-13 00:59
工具类可以贴出来么
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 22:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表