最近为了学英语买了个mp3,说是可以歌词同步显示的,后来从网上下载的歌词格式是如下
[ti:lesson 10]
[ar:新概念2册]
[al:]
[by:LRC之家 http://www.i162.com]
[00:02.01]Lesson 10
[00:07.19]Not for jazz
[00:10.11]First listen and then fabiao the question
[00:15.93]What happened to the clavichord?
[00:22.41]We have an old musical instrument.
[00:27.69]It is called a clavichord.
[00:31.37]It was made in Germany in 1681.
[00:36.19]Our clavichord is kept in the living room.
[00:39.44]It has belonged to our family for a long time.
[00:43.80]The instrument was bought by my grandfather many years ago.
[00:49.86]Recently it was damaged by a visitor.
[00:54.11]She tried to play jazz on it!
[00:57.77]She struck the keys too hard and two of the strings were broken.
[01:04.56]My father was shocked.
[01:07.98]Now we are not allowed to touch it.
[01:10.58]It is being repaired by a friend of my fathers
结果不识别,去找老板理论,他说如下格式可以识别
[00:02]Lesson 10
[00:07]Not for jazz
[00:10]First listen and then fabiao the question
[00:15]What happened to the clavichord?
[00:22]We have an old musical instrument.
[00:27]It is called a clavichord.
[00:31]It was made in Germany in 1681.
[00:36]Our clavichord is kept in the living room.
[00:39]It has belonged to our family for a long time.
[00:43]The instrument was bought by my grandfather many years ago.
[00:49]Recently it was damaged by a visitor.
[00:54]She tried to play jazz on it!
[00:57]She struck the keys too hard and two of the strings were broken.
[01:04]My father was shocked.
[01:07]Now we are not allowed to touch it.
[01:10]It is being repaired by a friend of my fathers
试了下,到还真是可以识别,可是我下的新概念英语和别的资料格式都是第一种,要一个一个文件改太恐怖了,所以想做个小软件把第一种格式转化为第二种格式,也就是把前面的头去掉,把时间的后两位也去掉。我的文件存放形式是文件夹中有文件夹,想直接选顶根目录后,就把里面所有的*.lrc格式的文件从第一种形似改成第二种。
因为本来一直不熟悉对文件夹和对文件这方面的编程,对这个也没有好的思路,所以想各位请教了,能提供思路的提供思路,能提供关键代码的提供关键代码,能提供程序的提供程序,小弟在此一一谢过!
按文本文件读取字符串再截取[00:前面的字符。
去头还不容易,用StreamReader读入文本,截了头再写入
帮你顶一下
查查StreamReader和StreamWriter的使用方法.
如果你是来学习的,请先搞清楚下面几件
1)C#的IO操作 —— 比起C/C++,超简单
2) 递归算法 —— 如果你是学计算机的还不会,该打!!
3)如果经过努力还不会以上两条 —— 以头抢地耳!
using System;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
class LrcConvert
{
static string FilePattern = "*.lrc";
static Regex Pat0 = new Regex(@"^\[[0-9]{2}:[0-9]{2}.[0-9]{2}\]"); // 转换前
static Regex Pat1 = new Regex(@"^\[[0-9]{2}:[0-9]{2}\]"); // 转换后
static void Main()
{
Console.WriteLine("搜索当前目录及所有子目录的{0}文件并转换格式", FilePattern);
DirWork(".");
Console.WriteLine("完成!");
}
// 对dir目录干活并递归搜索子目录
static void DirWork(string dir)
{
SearchFile(dir);
string [] dirs = Directory.GetDirectories(dir);
foreach (string d in dirs)
{
DirWork(d);
}
}
// 搜索文件
static void SearchFile(string dir)
{
try
{
string [] files = Directory.GetFiles(dir, FilePattern);
foreach (string filename in files)
{
Replace(filename);
}
}
catch {}
}
// 删除文件头并替换[HH:mm.ss]为[HH:mm]
static void Replace(string filename)
{
Console.Write(filename + " ... ");
ArrayList al = new ArrayList();
try
{
using (StreamReader sr = new StreamReader(filename))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (Pat1.IsMatch(line)) al.Add(line);
else if (Pat0.IsMatch(line)) al.Add(line.Remove(6,3));
}
}
using (StreamWriter sw = new StreamWriter(filename))
{
foreach (string line in al)
{
sw.WriteLine(line);
}
}
}
catch
{
Console.WriteLine("Fail!");
return;
}
Console.WriteLine("OK!");
}
}