是对一个txt文件进行操作
此文件里的内容是几行信息,1行空行
我是想把几行信息读出来然后写成一行到另一个txt文件里
例如:
123
aaa
789333
bbb
写完的效果是
123,aaa,789
333,bbb
先谢过各位了!
关键是如何对空行进行判断,我试了readline()=="",readline().lenth==0
都不行,可能我的思路根本就错了,望高人点拨!小弟学习中!
此文件里的内容是几行信息,1行空行
我是想把几行信息读出来然后写成一行到另一个txt文件里
例如:
123
aaa
789333
bbb
写完的效果是
123,aaa,789
333,bbb
先谢过各位了!
关键是如何对空行进行判断,我试了readline()=="",readline().lenth==0
都不行,可能我的思路根本就错了,望高人点拨!小弟学习中!
string str = "";
while((str =tr.ReadLine())!= null)
{
if(str.Length ==0)
this.textBox1.Text+="\r\n";
this.textBox1.Text+= str;
}
sr.Close();
然后
output=output.Replace(",","\r\n");
output=output.Replace(",",",,");
private void button1_Click(object sender, System.EventArgs e)
{
System.IO.StreamReader sr = System.IO.File.OpenText("a.txt");
string str = "";
while((str =sr.ReadLine())!= null)
{
if(str.Length !=0)
this.listBox1.Items.Add (str);
else
this.listBox1.Items.Add ("");
}
sr.Close(); }
然后将a.txt中的文件重新写入b.txt文件中
private void button2_Click(object sender, System.EventArgs e)
{
string temp = "";
string str = "";
if (System.IO.File.Exists ("b.txt"))
{
System.IO.File.Delete("b.txt");
} System.IO.TextWriter ww = System.IO.File.CreateText ("b.txt");
for (int i=0;i<this.listBox1.Items.Count;i++)
{
str = this.listBox1.Items[i].ToString ().Trim ();
if(str.Length !=0)
{
temp += str+",";
}
else
{
temp = temp.Substring (0,temp.Length -1);
temp +="\r\n"+str; } }
temp = temp.Substring (0,temp.Length -1);
ww.WriteLine (temp);
ww.Close ();
}