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();

解决方案 »

  1.   

    楼上的写的基本可以,只是有一个地方可能大意了.
    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();
    }