假设我有一个包含如下数据的文本文件,我想读取第一行并将元素存储在一个数组中。读取第二行并存储在第二个数组中,依此类推。稍后我将对数组进行一些操作。你能帮我在 C# 中做到这一点吗?
输入文本文件:
5,7,3,6,9,8,3,5,7
5,6,8,3,4,5
6,4,3,2,65,8,6,3,3,5,7,4
4,5,6,78,9,4,2,5,6
我正在尝试的代码是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace ReadFile
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void Main(string[] args)
{
TextReader tr = new StreamReader("Data.txt");
// write a line of text to the file
string word = tr.ReadLine();
//now split this line into words
string[] val = word.Split(new Char[] { ',' });
}
}
}
如果我使用上述技术,我可以得到数组 val 中的第一行。有没有办法为所有行循环它?