本文介绍了如何同时读取和写入文本文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
如何同时读取和写入文本文件.

我有以下代码,但问题是它确实写在文件内容的末尾.但我希望它写在两个"之后.即,如果读取的行包含两个",则在两个"之后写测试".

Hello,
How can i read and write a text file at the same time.

I have the following code but the problem is that it does writes at the end of the file contents. but i want it to write at after "two". i.e. if the read line contains "two" then write "test" after "two".

FileStream fsrw = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader sr = new StreamReader(fsrw);
StreamWriter sw = new StreamWriter(fsrw);
test = "Dummy";

 while (test != null)
{
 if (test.Contains("two"))
    {
    sw.Write("test");
    }
    test = sr.ReadLine();
}
sw.Close();
}

推荐答案


这篇关于如何同时读取和写入文本文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:21