Java codeimport 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"));
}
}
}