Readread 모드에서 열린 streamreader를 사용하여 파일을 읽습니다. 필자가 가지고있는 요구 사항은 특정 텍스트 파일을 확인하고 발견 된 경우 해당 줄을 새 줄로 바꾸는 것입니다.쓰기 중 파일의 라인을 덮어 쓰기
현재 나는 쓰기 위해 StreamWriter
을 초기화했습니다.
파일에 텍스트를 쓰고 있지만 새 줄에 추가하고 있습니다.
그래서 특정 줄 텍스트를 바꾸려면 어떻게해야합니까?
System.IO.FileStream oStream = new System.IO.FileStream(sFilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read);
System.IO.FileStream iStream = new System.IO.FileStream(sFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
System.IO.StreamWriter sw = new System.IO.StreamWriter(oStream);
System.IO.StreamReader sr = new System.IO.StreamReader(iStream);
string line;
int counter = 0;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("line_found"))
{
sw.WriteLine("line_found false");
break;
}
counter++;
}
sw.Close();
sr.Close();
코드에 질문을 붙여주십시오. – Matt