最简单的就是用 StreamReader 和StreamWriter 来读写txt文件 下面是个小例子,不明白请追问
private static void ReadInfoFromFile(string filePath)
{
if (File.Exists(filePath))
{
List<string> list = new List<string>();
// 打开文件时 一定要注意编码 也许你的那个文件并不是GBK编码的
using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("GBK")))
{
while (!sr.EndOfStream) //读到结尾退出
{
string temp = sr.ReadLine();
if (!list.Contains(temp)) //去除重复的行
{
list.Add(temp);
}
}
}
//写回去,第二个参数 Append = false ,就是说覆盖原来的
using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.GetEncoding("GBK")))
{
foreach (string line in list)
{
sw.WriteLine(line);
}
}
}
}
追问嗯。。我是想弄个软件通过读取文件然后根据文件数据进行计算,最好输出计算结果,这些就行了吗?
追答我这个只是提供给你一个例子,怎么去读写txt格式的文件,具体你要写什么软件看你自己的需求,一步一步慢慢来,相信你一定能写出来