替换文本文件中的某个单词

本文关键字:单词 文本 文件 替换 | 更新日期: 2023-09-27 18:32:47

我知道这已经被问过几次了,但是我已经看到了很多正则表达式等,我相信还有另一种方法可以只用流阅读器/写入器来做到这一点。下面是我的代码。我试图用"卷心菜"这个词代替"茶"。有人可以帮忙吗?我相信我的语法错误。

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();
        }
    }
}

替换文本文件中的某个单词

如果您不关心内存使用情况:

string fileName = @"C:'Users'Greg'Desktop'Programming Files'story.txt";
File.WriteAllText(fileName, File.ReadAllText(fileName).Replace("tea", "cabbage"));

如果您的多行文件不会在行尾随机拆分单词,则可以以更易于内存的方式一次修改一行:

// 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);

最后我用了这个: 希望它能帮助别人

      public List<string> EditorialResponse(string fileName, string searchString,                  string replacementString)
    {
        List<string> list = new List<string>();
        using (StreamReader reader = new StreamReader(fileName))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                line = line.Replace(searchString, replacementString);
                list.Add(line);
                Console.WriteLine(line);
            }
            reader.Close();
        }
        return list;
    }

}
class Program
{
    static void Main(string[] args)
    {
        TextImportEdit tie = new TextImportEdit();
        List<string> ls = tie.EditorialResponse(@"C:'Users'Tom'Documents'Visual Studio 2013'story.txt", "tea", "cockrel");
        StreamWriter writer = new StreamWriter(@"C:'Users'Tom'Documents'Visual Studio 2013'story12.txt");
        foreach (string line in ls)
        {
            writer.WriteLine(line);
        }
        writer.Close();

    }
}

}