不能劈线
本文关键字:不能 | 更新日期: 2023-09-27 18:10:45
我有这部分代码,它接受一个文件并将其放在ArrayList
中。将要输入的文件将是CSV
(我使用的当前CSV
在第一行有标题,所以我不需要那一行),第二行必须放在ArrayList
中。
我使用ArrayList
,因为文件可以是动态的,所以我不确定第二行的长度。我测试了(在第二行有7个逗号分隔值的文件)这段代码,它打印出ArrayList
的长度(fileList.Count
) = 1。
怎么了
ArrayList fileList2 = new ArrayList();
private void button3_Click(object sender, EventArgs e)
{
string filename = "";
DialogResult result = openFileDialog2.ShowDialog();
if (result == DialogResult.OK)
{
filename = openFileDialog2.FileName;
textBox3.Text = filename;
string line2;
System.IO.StreamReader file2 = new System.IO.StreamReader(textBox3.Text); //reads file from textbox
stringforData = file2.ReadLine(); // this reads the first line that I dont need
while ((line2 = file2.ReadLine()) != null) //read the lines
{
// puts elements into array
fileList2.Add(line2.Split(';'));//split the line and put it in the arraylist
}
file2.Close();
if (true) // this is for testind what is happening
{
this.textBox2.Clear();
textBox3.Text = Convert.ToString(fileList2.Count);
}
}
}
您不想使用fileList2.AddRange()而不是fileList2.Add()吗?在我看来,您现在正在向fileList中添加一个项目。该项是一个数组,其中包含您实际想要添加到列表中的所有项。如果你先得到那个数组,然后使用adrange方法,应该没问题。
首先,您可能应该使用adrange(),而不是Add()。其次,如果这是一个CSV文件,那么为什么要向split()方法传递分号?