正则表达式匹配.avi文件名

  • 一万年没有来这里写东西了,今天分享一下刚刚完成的一个小功能:也就是标题所述的功能咯。【Java 实现】
  • 背景是这样子的:我在网上下载了很多视频文件。都是.avi结尾的。而且已经给我排序了,视频文件名称都是
    1.xxx.avi
    2.xxx.avi
    ...
    9.xxx.avi
    10.xxx.avi
    11.xxx.avi
    ...
    1024.xxx.avi
  • 播放器是智能的,知道按顺序播放。但是播放器不够智能的是,不能区分 1.xxx.avi10.xxx.avi100.xxx.avi的顺序。必须要是

    01.xxx.avi
    02.xxx.avi
    ...
    09.xxx.avi
    10.xxx.avi
    ...
    1024.xxx.avi

    这样的命名之后,播放器才能按照01-1024的顺序播放。如果1-1024,播放器就傻逼了。顺序就乱掉了。,所以,我需要做的就是,将所有的1.xxx.avi-9.xxx.avi全部重命名01.xxx.avi -09.xxx.avi10 -1024开头的就不需要重命名了。

  • 我绞尽乳汁,终于想到了。关键代码就3行:

  • *
public static void main(String[] args) {
        boolean matches = "11.wererer.avi".matches("\\d\\..+\\.avi");
        System.out.println("----"+matches);
// ----false
        boolean matches2 = "1.wererer.avi".matches("\\d\\..+\\.avi");
        System.out.println("----"+matches2);
// ----true
    }
package com.pythoncat.hellomyeclipse;
import java.io.File;
import org.apache.commons.lang3.StringUtils;

public class FileRename {

    public static void main(String[] args) {
        reNameFiles("G:\\avi目录");
    }

    /** * 将视频文件1.xx.avi,2.xx.avi,...9.xx.avi,10.xx.avi 转换为01.xx.avi,02.xx.avi * ...10及以后的,不需要转换 * * @param dir */
    public static void reNameFiles(String dir) {
        System.out.println("输出 了。。。。。。。。。。。。。。。");
        boolean check = checkDirPath(dir);
        if (check) {
            File dirFile = new File(dir);
            if (dirFile.isDirectory()) {
                File[] files = dirFile.listFiles();
                for (File file : files) {
                    if (file.isDirectory()) {
                        reNameFiles(file.getPath());
                    } else {
                        reName(file);
                    }
                }
            } else {
                reName(dirFile);
            }

        }
    }

    private static void reName(File file) {
        // 1.SSSSSSSSSSSSSSS.avi

        String name = file.getName();
        if (!StringUtils.isEmpty(name)) {
            String rex = "\\d\\..+\\.avi";
            boolean matches = name.matches(rex);
            System.out.println("mat=====" + matches);
            if (matches) {
                // 说明是一个avi文件
                System.out.println("name = " + name);
                String path = file.getPath();
                File dest = new File(file.getParent(),"0"
                        + file.getName());
                boolean renameto = file.renameto(dest);
                System.out.println("renameto = " + renameto + "\treName = "
                        + dest.getName());
            }
        }
    }

    private static boolean checkDirPath(String dir) {
        if (StringUtils.isEmpty(dir)) {
            System.err.println("目标路径为空: " + dir);
            return false;
        }
        File dirFile = new File(dir);
        if (dirFile == null || !dirFile.exists()) {
            System.err.println("目标路径不存在: " + dir);
            return false;
        }
        return true;
    }
}

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...