StreamReader sr =File.OpenText(@"z:\x.txt");
System.Collections.ArrayList al=new System.Collections.ArrayList();
string temp="";
while(temp!=null)
{
temp=sr.ReadLine();
al.AddRange(temp.Split(' '));
}
sr.Close();
System.Collections.ArrayList al=new System.Collections.ArrayList();
string temp="";
while(temp!=null)
{
temp=sr.ReadLine();
al.AddRange(temp.Split(' '));
}
sr.Close();
解决方案 »
- winform 两时间控件之间值的查询
- 菜鸟问题 datatable
- 数据库连接问题,请高手指点!
- ICShapeZip 可以实现分卷压缩吗?如何实现?
- windows服务能打开一程序吗?
- log4net,用自定义配置文件名运行出错,请教
- 如何用私钥解密码?
- form窗体上有一个textbox控件,鼠标点一下textbox的时候,form窗体发生什么事件?
- 本机能运行,上传后报错,请同志们指点~急啊~~~~~~~~~~~~
- 高手释疑,关于.NET中的MD5加密,我用.NET中的函数算出的MD5值为何不对?!
- 如何给treeview的节点添加快捷菜单?
- 新手入门紧急求助:如何判断一个字符串是不是IP地址?
while(temp!=null)
{
temp=sr.ReadLine();
al.AddRange(temp.Split(' '));
}
注意这个循环,当读到最后一行g h i 3, tmp="g h i 3",由于temp!=null,所以会继续进入循环,这是temp=sr.ReadLine();产生的结果是tmp=null,再执行al.AddRange(temp.Split(' '));就会出错.这是我改造的,基本就是抄过来的.
StreamReader sr = null;
try
{
StreamReader sr =File.OpenText(@"z:\x.txt");
System.Collections.ArrayList al=new System.Collections.ArrayList();
string temp=sr.ReadLine();
while( temp!=null)
{
al.AddRange(temp.Split(' '));
temp=sr.ReadLine();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{ sr.Close();
}