4

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

sikas
  • 5,435
  • 28
  • 75
  • 120
user1849989
  • 147
  • 2
  • 2
  • 10

3 Answers3

8

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();
    }
}
Michael
  • 507
  • 5
  • 20
  • 1
    You need to add `reader.close()` or a `using` statement otherwise the file would be in use when you try to write into it. – Yoh Aug 03 '22 at 15:04
4
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(listItem);
           
        }
    }

examples about StreamWriter in C# Programming Guide

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

Sandeep
  • 83
  • 1
  • 3
Frank59
  • 3,141
  • 4
  • 31
  • 53
0

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 ...");
}