怎样用C#编写打开txt数据,进行读取数据,按照一定的规则重新存储数据
用streamreader读出来,自己处理,用streamwriter写回去
private void ReadConfigFile()
{
try
{
FileStream fs = new FileStream(configFilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
//使用StreamReader类来读取文件
sr.BaseStream.Seek(0, SeekOrigin.Begin);
//从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
while (true)
{
string tmp = sr.ReadLine();
if (tmp == null) break;
string[] str = tmp.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (str.Length != 2) continue;
switch (str[0].Trim())
{
case "DLLNAME":
_dllName = AppDomain.CurrentDomain.BaseDirectory + str[1].Trim() + ".dll";
break;
case "CLASSFULLNAME":
_classFullName = str[1];
break;
case "STATICFIELDNAME":
_FieldName = str[1];
break;
default:
break;
}
}
//关闭此StreamReader对象
sr.Close();
}
catch
{
}
}
读用reader,写用writer,注意关闭流
读取肯定如楼上的两位大神所说,streamreader,如果有需要用split将读取的字符串拆分成你需要存储的数据,用集合或者数组保存,然后streamwriter再写回去