Home > database >  How to create a method can read inputstream and replace new word to old word?
How to create a method can read inputstream and replace new word to old word?

Time:12-14

There is a replace method that reads the InputStream and replaces the old word with a new word, then it will return the output file when I replace the word.

public Stream ReplaceFile(Stream InputStream, string oldword, string newword)
        {
            StreamReader sr = new StreamReader(InputStream);
            string text = sr.ReadToEnd();
            
            StreamWriter sw = new StreamWriter(InputStream);
            sw.Write(text.Replace(oldword, newword));


            Stream output = File.Create(@"C:\Users\users\source\test\Data\2test.csv");

            return output;       
        }

CodePudding user response:

welcome SO. In my small tests using your code, I was unable to get it work... so in my tests it is NOT correct. In addition deleting your question then reposting the same question is frowned upon and you may have problems asking new questions later if you end up deleting/reposting a bunch of questions… just a thought.

It is not clear “why” the code “passes in” a Stream and then returns a different Stream… ? … This certainly will work; however it seems unnecessary in this context. One issue you may have, is using the “same” Stream for both the StreamReader and StreamWriter which the code appears to do. Even if the InputStream allows read and write access to the file… you will need to implement more code to get it to work as you are wanting. Basically, there is an easier way.

Lastly… when opening/reading/writing files… it becomes YOUR responsibility to “close” those files. It is not difficult to forget to do this and the code will never complain about it. To help, there is a statement called a “using” statement. IF whatever resource we want to open/read/write to implements the IDisposable interface… THEN (pardon the play on words) “using” a using statement will do most of what we need as far as “closing” the file or resource.

Given this, I am confident the code below should do as you are wanting. NOTE, the code does NOT take a Stream object nor return one. If you need this, then you may need to explain why.

Walking through the code… the method takes four (4) parameters, two string variables that define the source and destination file path and name. And two string variables that define the oldWord and newWord.

A string text is used to hold the text from the source file when it is read in addition to holding the same text with the replacement newWord.

Using a using statement to read the file and replace the text. Then another using statement to write the changes to a file.

I have done minor testing with this code and I am confident more error checking may be needed. Good Luck.

public static void ReplaceFile(String sourceFile, string destFile, string oldWord, string newWord) {
  string text;
  using (StreamReader sr = new StreamReader(sourceFile)) {
    text = sr.ReadToEnd();
    text = text.Replace(oldWord, newWord);
  }
  using (StreamWriter sw = new StreamWriter(destFile)) {
    sw.Write(text);
  }
}

Usage…

string sourcePath = @"C:\Users\users\source\test\Data\2test.csv";
string destPath = @"C:\Users\users\source\test\Data\2test.csv";
string oldWord = "oldWord";
string newWord = "newWord";
ReplaceFile(sourcePath, destPath, oldWord, newWord);
  •  Tags:  
  • c#
  • Related