处理后文本里显示换行符正常
1
2
3
复制到日志后
1
2
3
----------------------------------------------代码------------------------------------------------------
[C#] 纯文本查看 复制代码 //重新读取txt里面内容重新写出到文件
for (int n = 0; n < dataGridView1.RowCount; n++)
{
toolStripStatusLabel2.Text = "重新整理中";
StreamReader SReader = new StreamReader(dataGridView1.Rows[n].Cells[0].Value + "\\" + dataGridView1.Rows[n].Cells[1].Value, Encoding.Default);
str = SReader.ReadToEnd();
SReader.Close();
string[] arr2 = Regex.Split(str, "\r\n", RegexOptions.IgnoreCase);//以----分割字符串
string str1 = "";
for (int m = 0; m < arr2.Length;m++ )
{
if(arr2[m]!="")
{
str1 += arr2[m] + "\n";
}
}
string path = dataGridView1.Rows[n].Cells[0].Value + "\\" + dataGridView1.Rows[n].Cells[1].Value;
if (!File.Exists(path))
{
FileStream fs1 = new FileStream(path, FileMode.Create, FileAccess.Write);//FileMode.Create创建文件
StreamWriter sw = new StreamWriter(fs1, Encoding.Default);//对象与编码
sw.WriteLine(str1);//开始写入值
sw.Close();
fs1.Close();
}
else
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Write);//FileMode.Open打开
StreamWriter sr = new StreamWriter(fs, Encoding.Default);//对象与编码
sr.WriteLine(str1);//开始写入值
sr.Close();
fs.Close();
}
}
|