There is a replace function that can read stream and replace new word to old word. However, the replace function is not working:
static void Main(string[] args)
{
Stream filepath = File.OpenRead(@"C:\Users\users1\source\repos\ConsoleApp1\ConsoleApp1\Data\test.csv");
//List<string> oldstring = new List<string>(1);
//oldstring.Add("Y");
string oldstring = "N";
string newstring = "Y";
ReplaceMethod(filepath, oldstring, newstring);
}
static Stream ReplaceMethod(Stream path, string oldstring, string newstring)
{
StreamReader sr = new StreamReader(path);
string text = sr.ReadToEnd();
sr.Close();
StreamWriter sw = new StreamWriter(path);
sw.Write(text.Replace(oldstring, newstring));
sw.Close();
}
CodePudding user response:
Stream filepath = File.OpenRead(@"C:\Users\users1\source\repos\ConsoleApp1\ConsoleApp1\Data\test.csv");
you are opening filepath with File.OpenRead... but then trying to open StreamWriter with same thing. You need to use the actual path (string) instead of Stream.
Original Answer:
You have to close your Readers and writers for the text to be read and written successfully.
static void ReplaceMethod(string path, string oldstring, string newstring)
{
StreamReader sr = new StreamReader(path);
string text = sr.ReadToEnd();
sr.Close(); // <---- This closes the Reader
StreamWriter sw = new StreamWriter(path);
sw.Write(text.Replace(oldstring, newstring));
sw.Close(); // <--- This writes and closes the StreamWriter.
}
Purpose of StreamReader Close method
Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.
Purpose of StreamWriter Close method
You must call Close to ensure that all data is correctly written out to the underlying stream. Following a call to Close, any operations on the StreamWriter might raise exceptions. If there is insufficient space on the disk, calling Close will raise an exception.
UPDATE:
Found the other reason why your code is not working. You are passing in Stream
as the argument to the method... and this Stream was opened as Read
.
Stream filepath = File.OpenRead(...
You need to pass in the path to the file
itself to make it work (@"C:\Users\user1\source\repos\ConsoleApp1\ConsoleApp1\Data\test.csv"
). I tested the above code (using string path
as the argument ... not Stream path) successfully.
CodePudding user response:
You are using the same stream object to read and write the file. that is why stream is not writable so it is not writing the replaced text to file.
You can do something like this:
// See https://aka.ms/new-console-template for more information
using System.IO;
Console.WriteLine("Hello, World!");
string path = @"D:\test.txt";
//List<string> oldstring = new List<string>(1);
//oldstring.Add("Y");
string oldstring = "N";
string newstring = "Y";
ReplaceMethod(path, oldstring, newstring);
static void ReplaceMethod(string path, string oldstring, string newstring)
{
//StreamReader sr = new StreamReader(path);
//string text = sr.ReadToEnd();
//sr.Close();
string text = File.ReadAllText(path);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
sw.Write(text.Replace(oldstring, newstring));
sw.Close();
}