0

我想tempstring从 output.txt 文件中删除行

File.Copy(words, Npath);
StreamReader temp = new StreamReader(Npath, Encoding.UTF8);
string tempstring = temp.ReadToEnd();
string[] temp3 = tempstring.Split(',');
temp.Close();

StreamWriter sW = File.AppendText(Npath);
string remove = tempstring.Replace(tempstring," ");
foreach (string item in temp3)
{
    string str = item.TrimStart().TrimEnd();
    string newStr = "<addr-line>" + str + "</addr-line>";
    sW.WriteLine(newStr);
}
sW.Close();

输出 .txt 文件显示

Marko Kostic, 诺维萨德大学技术科学学院, Trg D. Obradovica 6, 21125 Novi Sad, 塞尔维亚

<addr-line>Marko Kostic</addr-line>
<addr-line>Faculty of Technical Sciences</addr-line>
<addr-line>University of Novi Sad</addr-line>
<addr-line>Trg D. Obradovica 6</addr-line>
<addr-line>21125 Novi Sad</addr-line>
<addr-line>Serbia</addr-line>

我想删除代码中tempstring来自输出文件的粗体段落

有什么建议么?

4

2 回答 2

0

why are using append?

//StreamWriter sW = File.AppendText(Npath);
StreamWriter sW = new StreamWriter(Npath);

this will remove the old content (which you read before) and set the new one.

于 2013-03-24T08:35:48.007 回答
0

代替

StreamWriter sW = File.AppendText(Npath);

StreamWriter sW = new StreamWriter(Npath, false);

因为,您想覆盖文件中的文本,所以不要以附加模式打开文件,您应该创建一个新的StreamWriter并传递false参数,该参数将为append选项设置 false 值。

于 2013-03-24T08:45:21.887 回答