How to edit a text file in c# with StreamWriter?

青春壹個敷衍的年華 提交于 2019-12-11 04:03:37

问题


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:

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:

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


回答1:


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



回答2:


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 StreamWriter in C# Programming Guide

How to: Write to a Text File (C# Programming Guide)




回答3:


You can have just this simple code, and it should edit your "filename.txt" and append new text.

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\TestFolder\filename.txt", true))
{
file.WriteLine("text to edit / append ...");
}


来源:https://stackoverflow.com/questions/13552938/how-to-edit-a-text-file-in-c-sharp-with-streamwriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!