替换文本文件中的某个单词 [英] Replacing a certain word in a text file
本文介绍了替换文本文件中的某个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这已经被问了几次了,但是我看到了很多正则表达式等等,而且我确信还有另外一种方法可以用流读/写器来做到这一点。以下是我的代码。我试图用白菜来代替茶。有人可以帮忙吗?
namespace Week_9_Exer_4
{
class TextImportEdit
{
public void EditorialControl()
{
string fileName;
string lineReadFromFile;
Console.WriteLine();
//询问要读取的文件的名称
Console.Write(你想读哪个文件?);
fileName = Console.ReadLine();
Console.WriteLine();
//打开文件以读取
StreamReader fileReader = new StreamReader(C:\\ Users \\Greg\\Desktop\\Programming Files\\ \\\story.txt);
//读取文件中的行并显示它们
//直到返回null(表示文件的结尾)
lineReadFromFile = fileReader.ReadLine();
Console.WriteLine(请输入您要编辑的字词:);
string editWord = Console.ReadLine();
while(lineReadFromFile!= null)
{
Console.WriteLine(lineReadFromFile);
lineReadFromFile = fileReader.ReadLine();
}
String text = File.ReadAllText(C:\\ Users \\Greg\\Desktop\\Programming Files\\\\\\\\\文本);
fileReader.Close();
StreamWriter fileWriter = new StreamWriter(C:\\ Users \\Greg\\\\Desktop\\ Program Files \\\\\\\\\\\\\\\\'
string newText = text.Replace(tea,cabbage);
fileWriter.WriteLine(newText);
fileWriter.Close();
$ b 解决方案 如果你不关心内存使用情况:
string fileName = @C:\\ \\用户\ Greg \Desktop\Programming Files\story.txt;
File.WriteAllText(fileName,File.ReadAllText(fileName).Replace(tea,cabbage));
如果您有一个多行文件,它不会在行尾随机地分割单词,你可以用更友好的方式一次修改一行:
//为源文件打开一个流
using(var sourceFile = File.OpenText(fileName))
{
//创建一个临时文件路径,我们可以在这里写修改行
string tempFile = Path.Combine(Path .GetDirectoryName(fileName),story-temp.txt);
//使用(var tempFileStream = new StreamWriter(tempFile))为临时文件
打开一个流
{
string line;
//读取文件中的行
while((line = sourceFile.ReadLine())!= null)
{
//替换单词
线= line.Replace(茶,cabbage);
//将修改过的行写入新文件
tempFileStream.WriteLine(line);
$ b //将原始文件替换为临时文件
File.Replace(story-temp.txt,story.txt , 空值);
I know this has been asked a few times, but I have seen a lot of regex etc., and I'm sure there is another way to do this with just a stream reader/writer. Below is my code. I'm trying to replace "tea" with the word "cabbage". Can somebody help? I believe I have the wrong syntax.
namespace Week_9_Exer_4
{
class TextImportEdit
{
public void EditorialControl()
{
string fileName;
string lineReadFromFile;
Console.WriteLine("");
// Ask for the name of the file to be read
Console.Write("Which file do you wish to read? ");
fileName = Console.ReadLine();
Console.WriteLine("");
// Open the file for reading
StreamReader fileReader = new StreamReader("C:\\Users\\Greg\\Desktop\\Programming Files\\story.txt");
// Read the lines from the file and display them
// until a null is returned (indicating end of file)
lineReadFromFile = fileReader.ReadLine();
Console.WriteLine("Please enter the word you wish to edit out: ");
string editWord = Console.ReadLine();
while (lineReadFromFile != null)
{
Console.WriteLine(lineReadFromFile);
lineReadFromFile = fileReader.ReadLine();
}
String text = File.ReadAllText("C:\\Users\\Greg\\Desktop\\Programming Files\\story.txt");
fileReader.Close();
StreamWriter fileWriter = new StreamWriter("C:\\Users\\Greg\\Desktop\\Programming Files\\story.txt", false);
string newText = text.Replace("tea", "cabbage");
fileWriter.WriteLine(newText);
fileWriter.Close();
}
}
}
解决方案 If you don't care about memory usage:
string fileName = @"C:\Users\Greg\Desktop\Programming Files\story.txt";
File.WriteAllText(fileName, File.ReadAllText(fileName).Replace("tea", "cabbage"));
If you have a multi-line file that doesn't randomly split words at the end of the line, you could modify one line at a time in a more memory-friendly way:
// Open a stream for the source file
using (var sourceFile = File.OpenText(fileName))
{
// Create a temporary file path where we can write modify lines
string tempFile = Path.Combine(Path.GetDirectoryName(fileName), "story-temp.txt");
// Open a stream for the temporary file
using (var tempFileStream = new StreamWriter(tempFile))
{
string line;
// read lines while the file has them
while ((line = sourceFile.ReadLine()) != null)
{
// Do the word replacement
line = line.Replace("tea", "cabbage");
// Write the modified line to the new file
tempFileStream.WriteLine(line);
}
}
}
// Replace the original file with the temporary one
File.Replace("story-temp.txt", "story.txt", null);
这篇关于替换文本文件中的某个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文