static void Main(string[] args)
{
try
{
//读取文本的所有行数据
string[] Lines=File.ReadAllLines("C:\\Demo.txt");
for(int i=0;i<Lines.Length;i++) //循环取出每一行
{
// 用于保存数据的字典
Dictionary<string, int> result = new Dictionary<string, int>();
foreach (char chr in Lines[i].ToCharArray()) //读取每行的每一个字
{
//如果字典中已包含了这个字
if (result.ContainsKey(chr.ToString()))
result[chr.ToString()] += 1;//数量增加1
else
result.Add(chr.ToString(),1);//否者存放到字典中
}
string end=string.Format("第{0}行:",i+1);//要存的数据头
foreach(KeyValuePair<string, int> kvp in result)//读取字点中的结果
{
end+=string.Format("[{0}]字出现了{1}次,\t",kvp.Key,kvp.Value);
}
File.AppendAllText("C:\\Result.txt",end); //将结果保存到新的文本中。
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}