StreamWriter如何循环追加到指定文件?
StreamReader sr = new StreamReader(@"C:\Users\xwy\Desktop\123.txt", Encoding.GetEncoding("GB2312"));
StreamWriter sw = new StreamWriter(@"C:\Users\xwy\Desktop\T.txt", true);
while (sr.EndOfStream)
{
string row = sr.ReadLine();
string[] columns = row.Split(new char[] { ',' });
sw.WriteLine("xxxxxx{0}xxxx{1}xxxx",columns [1],columns [2]);
sw.Flush();
sw.Close();
}
先读取123.txt的每一行,然后用,分割成一块块,接着输出。。。请问这代码哪里错了,我只生成了文件,没写入内容
------解决方案--------------------
代码如下,测试通过
C# code
StreamReader sr = new StreamReader(@"C:\Users\xwy\Desktop\123.txt", Encoding.GetEncoding("GB2312"));
StreamWriter sw = new StreamWriter(@"C:\Users\xwy\Desktop\T.txt", true);
while (!sr.EndOfStream)
{
string row = sr.ReadLine();
string[] columns = row.Split(new char[] { ',' });
sw.WriteLine("xxxxxx{0}xxxx{1}xxxx", columns[0], columns[1]);
}
sw.Flush();//关闭不要放到循环里面
sw.Close();