如何使用 StreamWriter 在 c# 中编辑文本文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13552938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to edit a text file in c# with StreamWriter?
提问by user1849989
I have a method to edit a word from a text file file and display it on the command prompt. Now I an trying to make a method to edit a word from a text file and write it to a new file. I would also like to mention that I cannot use the File or Regex class since I am not allowed to use it for my assignment. Here is my code for the StreamReader:
我有一种方法可以从文本文件文件中编辑单词并将其显示在命令提示符上。现在我尝试制作一种方法来编辑文本文件中的单词并将其写入新文件。我还想提一下,我不能使用 File 或 Regex 类,因为我不允许在我的作业中使用它。这是我的 StreamReader 代码:
public void EditorialControl(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Replace(word, replacement);
list.Add(line);
Console.WriteLine(line);
}
reader.Close();
}
}
and here is my code so far for the StreamWriter:
到目前为止,这是我的 StreamWriter 代码:
public void EditorialResponse(string fileName, string word, string replacement, string saveFileName)
{
using (StreamWriter writer = new StreamWriter(directory + saveFileName, true))
{
{
string input = directory + fileName;
string line = input.Replace(word, replacement);
writer.Write(line);
}
writer.Close();
}
}
what can I add to make the StreamWriter open a file, edit a word and write it to a new file or possibly use the StreamReader method to make these changes in StreamWriter? Thank you
我可以添加什么来使 StreamWriter 打开文件、编辑单词并将其写入新文件或可能使用 StreamReader 方法在 StreamWriter 中进行这些更改?谢谢
采纳答案by Michael
Here...
这里...
public void EditorialResponse(string fileName, string word, string replacement, string saveFileName)
{
StreamReader reader = new StreamReader(directory + fileName);
string input = reader.ReadToEnd();
using (StreamWriter writer = new StreamWriter(directory + saveFileName, true))
{
{
string output = input.Replace(word, replacement);
writer.Write(output);
}
writer.Close();
}
}
回答by Frank59
public IList<string> GetEditedContent(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Replace(word, replacement);
list.Add(line);
Console.WriteLine(line);
}
reader.Close();
}
return list;
}
// Example how write list to a file.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
{
foreach (string listItem in list)
{
file.WriteLine(line);
}
}
examples about StreamWriterin C# Programming Guide
有关实例的StreamWriter在C#编程指南
回答by Fernando José Cassola Marques
You can have just this simple code, and it should edit your "filename.txt" and append new text.
您可以拥有这个简单的代码,它应该编辑您的“filename.txt”并附加新文本。
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\TestFolder\filename.txt", true))
{
file.WriteLine("text to edit / append ...");
}

