日期:2014-05-20  浏览次数:20757 次

分享 :java实现 歌词文件的智能命名
自己写了个程序,从某台歌词服务器上下载了近一万首歌词(二十个线程,不到一个小时的时间)但是歌词的文件名都是数字 ,类似1.lrc、2.lrc ……

歌词文件内容格式如下:
Assembly code

[ar:阿杜]
[ti:黄昏]
[by:KG940521314]
[hash:845f72c1f9a16c520463846e203039d2]
[total:260780]
[00:22.31] 下着雨的天气
[00:25.54] 好象你可爱的坏脾气
[00:30.35] 我的爱我的心
[00:33.55] 早就已经交给了你
[00:37.42] 你说我很不错
[00:40.72] 是不是在暗示什么



其中[ar:] 里面的是 歌曲的演唱者 [ti:]里面的是个曲名。不过有些没有[ar:] 或[ti:]标记
现在想把这些歌词文件 用其内容 命名成 【演唱者 - 歌曲名】 的格式,以下是java的实现
Java code

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;

public class Rename {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("d:/aaa/"); //歌词存放路径

        long start = System.currentTimeMillis();
        File[] fList = file.listFiles(new FilenameFilter() { //过滤文件,只处理 lrc格式的

            @Override
            public boolean accept(File dir, String name) {
                if (!name.endsWith(".lrc"))
                    return false;
                return true;
            }
        });

        byte[] buf = new byte[1000];
        for (int i = 0; i < fList.length; i++) {
            try {
                FileInputStream fis = new FileInputStream(fList[i]);
                int length = fis.read(buf, 0, buf.length); //将文件开头的1000个字节读入到buf里
                fis.close();
                String tmp = new String(buf, 0, length);
                
                rename(fList[i], tmp); // 重命名
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        
        long usedTime  = System.currentTimeMillis()-start;
        
        System.out.println("用时:"+usedTime);
    }

    public static void rename(File file, String str) {
        String fullname = "";
        String artist = "";
        String name = "";
        if (-1 != str.indexOf("[ar:")) {
            String tmp = str.substring(str.indexOf("[ar:"), str.length());
            artist = tmp.substring(0, tmp.indexOf("]"))
                    .replaceAll("\\[ar:", "");
            if (-1 != str.indexOf("[ti:")) {
                String tmp2 = str.substring(str.indexOf("[ti:"), str.length());
                name = tmp2.substring(0, tmp2.indexOf("]")).replaceAll(
                        "\\[ti:", "");
                fullname = artist + " - " + name;
            } else {
                fullname = artist + " - 未知歌名";
            }
        } else {
            if (-1 != str.indexOf("[ti:")) {
                String tmp2 = str.substring(str.indexOf("[ti:"), str.length());
                name = tmp2.substring(0, tmp2.indexOf("]")).replaceAll(
                        "\\[ti:", "");
                fullname = "未知歌手  - " + name;
            }
        }

        if (!"".equals(fullname)) {
            file.renameTo(new File(file.getParent() + fullname + ".lrc"));
        }
    }

}



经实际测试,处理近10000个文件 实际用时 不到一秒!

------解决方案--------------------
hao !!!good
学习了
------解决方案--------------------
感觉在解析的时候有点小复杂,可以不用replaceAll
判断后直接截取‘:’到']'的字符串就好了
我用类似的方法解析并动态显示歌词,思路差不多是一样的。