Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用列表存储来自BufferedReader文件的数据_C#_List_Csv_Store_Bufferedreader - Fatal编程技术网

C# 使用列表存储来自BufferedReader文件的数据

C# 使用列表存储来自BufferedReader文件的数据,c#,list,csv,store,bufferedreader,C#,List,Csv,Store,Bufferedreader,我一直在尝试使用列表来存储.csv文件中的数据,但由于我不熟悉使用列表,我似乎无法让它工作。我搜索了很多教程,但没有一本适合我。我一定错过了什么。以下是我目前掌握的情况: int row = 0; int col = 0; String fileInput = JOptionPane.showInputDialog(null, "Please enter the path of the CSV file to r

我一直在尝试使用列表来存储.csv文件中的数据,但由于我不熟悉使用列表,我似乎无法让它工作。我搜索了很多教程,但没有一本适合我。我一定错过了什么。以下是我目前掌握的情况:

        int row = 0;
        int col = 0;

        String fileInput = JOptionPane.showInputDialog(null, 
                "Please enter the path of the CSV file to read:");

        File file = new File(fileInput);

        BufferedReader bufRdr;
        bufRdr = new BufferedReader(new FileReader(file));
        String line = null;

        ArrayList<String[]> arrayOfNumbers = new ArrayList<String[]>();

        //read each line of text file
        while((line = bufRdr.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line,",");
            col=0;

            while (st.hasMoreTokens()) {
                //get next token and store it in the array
                // UNSURE WHAT TO PUT HERE
            }
            row++;
        }
int行=0;
int col=0;
String fileInput=JOptionPane.showInputDialog(null,
“请输入要读取的CSV文件的路径:”;
文件文件=新文件(文件输入);
BufferedReader bufRdr;
bufRdr=newbufferedreader(newfilereader(file));
字符串行=null;
ArrayList ArrayOfNumber=新的ArrayList();
//读取文本文件的每一行
而((line=bufRdr.readLine())!=null){
StringTokenizer st=新的StringTokenizer(行,“,”);
col=0;
而(st.hasMoreTokens()){
//获取下一个令牌并将其存储在数组中
//不知道该在这里放什么
}
行++;
}

然后我想将该列表中的数据存储到一个名为
String[][]numbers
的多维数组中。你知道我应该怎么做吗?

在初始化多维字符串数组之前,你需要知道它的维数,它与你的最大列大小类似(如果行没有相同数量的“标记”),以及行数,也就是你拥有的行数

    int row = 0;
    int col = 0;

    String fileInput = JOptionPane.showInputDialog(null, 
            "Please enter the path of the CSV file to read:");

    File file = new File(fileInput);

    BufferedReader bufRdr;
    bufRdr = new BufferedReader(new FileReader(file));
    String line = null;

    ArrayList<StringTokenizer> arrayOfNumbers = new ArrayList<StringTokenizer>();

    //read each line of text file
    while((line = bufRdr.readLine()) != null) {
        StringTokenizer st = new StringTokenizer(line,",");
        arrayOfNumbers.add(st);

        int actColCount = st.countTokens();

        if (col < actColCount) {
             col = actColCount;
        }
    }

    row = arrayOfNumbers.size();
int行=0;
int col=0;
String fileInput=JOptionPane.showInputDialog(null,
“请输入要读取的CSV文件的路径:”;
文件文件=新文件(文件输入);
BufferedReader bufRdr;
bufRdr=newbufferedreader(newfilereader(file));
字符串行=null;
ArrayList ArrayOfNumber=新的ArrayList();
//读取文本文件的每一行
而((line=bufRdr.readLine())!=null){
StringTokenizer st=新的StringTokenizer(行,“,”);
数组编号。添加(st);
int actColCount=st.countTokens();
if(col
现在可以迭代存储的行并将它们放入数组中。可能出现了一些绑定异常的排列,但并没有太详细。但这应该可以

    String[][] numbers = new String[row][col];

    for (int i=0; i<row; i++) {

        StringTokenizer st = arrayOfNumbers.get(i);

        int j = 0;
        while(st.hasMoreElements()) {
             numbers[i][j] = st.nextElement();
             j++;
        }
    }
String[][]number=新字符串[行][col];
对于(int i=0;i