V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
KingEngine
V2EX  ›  程序员

Markdown 笔记清除冗余文件的小方法 Java

  •  
  •   KingEngine · 2018-10-22 18:55:03 +08:00 · 1824 次点击
    这是一个创建于 2004 天前的主题,其中的信息可能已经有所发展或是发生改变。

    个人记笔记所用微盘同步助手即时同步文件,图片文件也是设置默认保存在笔记目录下。markdown 有一个缺点就是纯文本,文档中图片为链接方式,当链接删除,源文件仍会存在于图片文件夹,下列这个程序可以对比并删除冗余的图片或资源文件(假删除,冗余文件将会移动到笔记文件夹中到 回收站 文件夹中,确定要删除时可以将整个文件夹手动删除)。上午问过大家正则,但是真的不怎么会,所以,,还是用最简单的方法展现吧。。大牛可以修改修改甚至弄个桌面程序出来蛤,提前祝 1024 快乐!

    public class Main {
        static ArrayList<String> list= new ArrayList<>();
        static String text;
        public static void main(String[] args) {
            File file = new File("D:\\微云同步助手\\QQ\\笔记");        //获取其 file 对象
          //获取其 file 对象
            func(file);
            for (String s : list) {
                String fileName = s.substring(s.lastIndexOf("\\") + 1, s.length());//获取文件名
                if (!text.contains(fileName)){ //如果图片等资源在 md 内容中不存在即删除
                    System.out.println(fileName+"删除成功");
                    String hs=file+"\\回收站\\";
                    if (!new File(hs).exists())
                        new File(hs).mkdir();
                    new File(s).renameTo(new File(hs+fileName));
                }
            }
        }
        /**
         * 遍历目录
         * @param file
         */
        private static void func(File file) {
            File[] fs = file.listFiles();
            for (File f : fs) {
                if (f.isDirectory() && !f.toString().contains("回收站"))    //排除回收站目录
                    func(f);
                if (f.isFile()) {      //若是文件,直接打印详细路径
                    String s = f.toString();
                    if (s.endsWith(".md")) {//获取 md 文件内容
                        text += readToString(s);
                    } else {
                        list.add(s);
                    }
     
                }
            }
        }
        /**
         * 获取文本
         * @param fileName
         * @return
         */
        public static String readToString(String fileName) {
            String encoding = "UTF-8";
            File file = new File(fileName);
            Long filelength = file.length();
            byte[] filecontent = new byte[filelength.intValue()];
            try {
                FileInputStream in = new FileInputStream(file);
                in.read(filecontent);
                in.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                return new String(filecontent, encoding);
            } catch (UnsupportedEncodingException e) {
                System.err.println("The OS does not support " + encoding);
                e.printStackTrace();
                return null;
            }
        }
    }
    
    9 条回复    2018-10-30 19:10:14 +08:00
    kawowa
        1
    kawowa  
       2018-10-22 19:13:20 +08:00 via Android
    天书啊!
    KingEngine
        2
    KingEngine  
    OP
       2018-10-22 19:23:57 +08:00
    @kawowa 不至于吧,就一个遍历 一个读取文本文件
    OpenJerry
        3
    OpenJerry  
       2018-10-22 19:25:58 +08:00 via Android
    1 楼用 v2er 看的吧,这个 app 代码显示有问题
    ywcjxf1515
        4
    ywcjxf1515  
       2018-10-22 19:51:12 +08:00 via iPad
    readToString(...)看着好难受,core java 第二卷了解一下。至少,readToString(...)里抛出了异常,之后的 close()执行不了吧。逃...
    aristotll
        5
    aristotll  
       2018-10-22 21:24:49 +08:00
    建议用 utils 如 Apache 或者 guava
    CSM
        6
    CSM  
       2018-10-22 21:41:44 +08:00 via Android
    直接拿图片文件名在整个 md 里找一下吗。。有点粗暴,就算用正则也不是很好。

    这个需求我能想到最好的解决办法是,先需要把 md 转为 html,然后就可以用众多的爬虫工具(比如 BeautifulSoup)了,用 img 标签来匹配,可以精确的多。
    KingEngine
        7
    KingEngine  
    OP
       2018-10-22 22:01:33 +08:00
    @CSM 嗯😄这是最简单的,将遍历出的 md 文件拼接成一个字符串暴力查找,几十几百个笔记还行吧,毕竟自己做的笔记单个文件不大,只是如果文件一多或一大,效率跟准确肯定跟不上,15M 文本要十几秒
    @ywcjxf1515 在我看来能跑就行,欢迎优化
    kawowa
        8
    kawowa  
       2018-10-22 23:08:29 +08:00 via Android
    @KingEngine 代码在 v2er 下拧成一团了...
    @OpenJerry 的确是在 v2er 下看到的哈哈
    KingEngine
        9
    KingEngine  
    OP
       2018-10-30 19:10:14 +08:00
    改进下,换成 StringBuilder 拼接字符串提高效率,233333333333 ……
    改进下:
    ```java
    public class Main {
    static ArrayList<String> list= new ArrayList<>();
    static StringBuilder text=new StringBuilder();

    /**
    * 暴力比较
    * @param args
    * @throws IOException
    */
    public static void main(String[] args) throws IOException {
    long start = System.currentTimeMillis();
    File file = new File("D:\\坚果云\\CodeNote"); //获取其 file 对象
    System.out.println("正在处理中……");
    func(file);
    for (String s : list) {
    String fileName = s.substring(s.lastIndexOf("\\") + 1, s.length());//获取文件名
    if (!text.toString().contains(fileName)){ //如果图片等资源在 md 内容中不存在即删除
    System.out.println(fileName+"删除成功 ");
    String hs=file+"\\回收站\\";
    if (!new File(hs).exists())
    new File(hs).mkdir();
    new File(s).renameTo(new File(hs+fileName));
    }
    }
    long end=System.currentTimeMillis();
    long time=end-start;
    System.out.println("已完成,耗时"+time+" ms, 请按任意键退出");
    System.in.read();
    }
    /**
    * 遍历目录
    * @param file
    */
    private static void func(File file) throws IOException {
    File[] fs = file.listFiles();
    for (File f:fs) {
    if (f.isDirectory() && !f.toString().contains("回收站")) //排除回收站目录
    func(f);
    if (f.isFile()) { //若是文件,直接打印详细路径
    String s = f.toString();
    if (s.endsWith(".md")) {//获取 md 文件内容
    text.append(readToString(s));
    System.out.println("读取"+text.length()/1000+"K 个字符");
    } else {
    list.add(s);
    }
    }
    }
    }
    /**
    * 获取文本
    * @param fileName
    * @return
    */
    public static String readToString(String fileName) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String line;
    StringBuilder txt = new StringBuilder();
    while ((line = br.readLine()) != null) {
    txt.append(line);
    }
    br.close();
    return txt.toString();
    }
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5970 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:15 · PVG 14:15 · LAX 23:15 · JFK 02:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.