使用Java将CSV文件内容读入多维数组

时间:2015-12-03 21:08:22

标签: java csv multidimensional-array

我正在使用Java编写登录屏幕GUI,允许用户登录系统。该程序引用一个csv文件,其中包含登录详细信息,它从中收集数据。

这与我看到的另一个问题类似,how to read csv into multidimensional array in Python

但就我而言,它是用Java而不是Python

我正在尝试从csv文件中读取数据,这些文件在Excel中创建为多维数组。我理解如何将其读入数据变量,这是为文本文件的每一行完成的。但是对于try catch块中的代码,它意味着将每行的每个字段分配给usersData [] []数组的元素,只是给了我错误。

我已经尝试了所有我知道的解决方法,但没有成功。

public static boolean determineifValid(String userID, String password){
    boolean detailsValid = false;
    // The name of the file 
    String file_Name = "PathologyInterfaceUsers.csv";
    File file = new File(file_Name);
    // The following code block reads the text file in the file_name variable. 
    String usersData[][] = new String[12][3];
    // CSV - comma seperated values 
    String data;
    int firstChar;
    int field_No;
    int i;
    int j;
    i = 0;
    try{            
        Scanner inputStream = new Scanner(file);
        while (inputStream.hasNext()){
            data = inputStream.next();
            // If the data record contains nothing that indicates the end of the file 
            if(data.equalsIgnoreCase("")){
                // Nothing happens and nothing needs to be done 
                System.out.println("End of file reached ");
            }else{

            if(i == 0){
            System.out.println("Record No: "+data);
            }else{
                System.out.println(i+"        : "+data);
            }
            field_No = 0;
            firstChar = 0;
            for(j = 0; j < data.length(); j++){
                /* If the current character is a comma separator, then the end of 
                *  the data field has been reached. 
                */
                if((data.charAt(j)) == ','){
                    System.out.println("Field "+(field_No + 1)+" is being copied to"
                            + " the data array");
                    usersData[i][field_No] = data.substring(firstChar, (j-1));

                    System.out.println("The data for field "+(field_No + 1)+"has been "
                            + "assigned to the "+ (field_No + 1) +" column");
                    // The index of the 2nd element of usersData, 'field' is incremented by 1 
                    field_No = field_No + 1;
                    System.out.println("The field_No has been incremented by 1");
                    /* The first character of the next field is the one after the 
                     * comma (value divider)
                    */
                    firstChar = j + 1;
                    System.out.println("Value divider ','");
                }else if(j == data.length()){
                    usersData[i][field_No] = data.substring(firstChar, j);
                }
            }
            i = i + 1;
            }

        }
        inputStream.close();
    }catch (FileNotFoundException e){
        System.out.println("We are so sorry, but an error has occured! ");
    }

1 个答案:

答案 0 :(得分:-1)

Csv的处理方式可能比看起来更复杂。只需使用专用于此的库,例如uniVocity-parsers,就可以避免头痛。这是一个简单的例子:

CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial.
CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new FileReader(file));

披露:我是这个图书馆的作者。它是开源和免费的(Apache V2.0许可证)。