日期:2014-05-20 浏览次数:20792 次
[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] 是不是在暗示什么
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")); } } }