大神们 指点指点 关于文件流的问题
FileStream afs = new FileStream(@"D:\a.txt", FileMode.OpenOrCreate);
FileStream bfs = new FileStream(@"E:\b.txt", FileMode.Create);
StreamWriter wafs = new StreamWriter(afs);
StreamReader rafs = new StreamReader(afs);
StreamWriter wbfs = new StreamWriter(bfs);
string str = Console.ReadLine();
wafs.Write(str);
wafs.Flush();
string rstr = rafs.ReadToEnd();
//Console.WriteLine("_______________");
Console.WriteLine(rstr); //控制台没有字符串输出
wbfs.Write(rstr); //b.txt里面是空的
wbfs.Flush();
Console.ReadLine();
我想先向a.txt写入数据,然后读出里面的数据再写入b.txt 谢谢了
------解决方案--------------------FileStream afs = new FileStream(@"D:\a.txt", FileMode.OpenOrCreate);
FileStream bfs = new FileStream(@"E:\b.txt", FileMode.Create);
StreamWriter wafs = new StreamWriter(afs);
StreamWriter wbfs = new StreamWriter(bfs);
StreamReader rafs = new StreamReader(afs);
string str = Console.ReadLine();
wafs.Write(str);//将字符写入流
wafs.Flush();//结束写入
afs.Position = 0;//写入a.txt后需要把流的起点设置下,这样rafs读出来的才不是空的
string rstr = rafs.ReadToEnd();
//Console.WriteLine("_______________");
Console.WriteLine(rstr);??//控制台没有字符串输出
wbfs.Write(rstr);?//b.txt里面是空的
wafs.Flush();
wbfs.Flush();