Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 663
user1849989 Avatar asked Oct 16 '25 09:10

user1849989


2 Answers

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();
    }
}
like image 62
Michael Avatar answered Oct 18 '25 23:10

Michael


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)

like image 23
Frank59 Avatar answered Oct 18 '25 23:10

Frank59



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!