我有这部分代码需要一个文件,并将其放入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);
}
}
}
2011年为什么要使用ArrayList?它已被弃用多年,现在,如果Baszz是正确的,你的例子显示了为什么 – Dyppl
要增加什么Dyppl说,看到[这个问题](http://stackoverflow.com/questions/5063156/why-isnt-arraylist-marked -obsolete)。 – R0MANARMY
@Dyppl我应该使用什么和如何? –