在 Java 中将数据从 CSV 解析为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6527358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:40:29  来源:igfitidea点击:

Parsing Data from CSV to Array in Java

javacsvstringtokenizer

提问by Roger Chen

I'm trying to import a CSV file into an array that I can use within a Java program. The CSV file has successfully imported itself and the output appears on Terminal but it throws the error:

我正在尝试将一个 CSV 文件导入到我可以在 Java 程序中使用的数组中。CSV 文件已成功导入,输出显示在终端上,但会引发错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 
at CompareCSV.main(CompareCSV.java:19)

at the end. In addition, when I try to call up elements in the array, it also shows the same error. My code is below:

在末尾。此外,当我尝试调用数组中的元素时,它也显示相同的错误。我的代码如下:

import java.io.*;
import java.util.*;

public class CompareCSV {

    public static void main(String[] args) {

        String fileName = "sampledata1.csv";
        try {
            BufferedReader br = new BufferedReader( new FileReader(fileName));
            String strLine = null;
            StringTokenizer st = null;
            int lineNumber = 0, tokenNumber = 0;

            while((fileName = br.readLine()) != null) {
                lineNumber++;
                String[] result = fileName.split(",");
                for (int x=0; x<result.length; x++) {
                    System.out.println(result[x]);
                }
            }
        }

        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }   
}

采纳答案by Mathias Schwarz

You are much better off using a proper CSV parser than hacking a faulty one up yourself: http://opencsv.sourceforge.net/

使用适当的 CSV 解析器比自己破解一个有问题的解析器要好得多:http: //opencsv.sourceforge.net/

CSV is not the simple format one might be let to think (yes, a line can contain a ,that does not separate two pieces of data).

CSV 不是人们可能会想到的简单格式(是的,一行可以包含,不分隔两条数据的 a)。

回答by Andreas Dolk

Looks like your assumption, that a line in the file alwayshas three columns isn't true for all lines. Replace the for loop statement with the following line to eliminate the exception and see, why it happend:

看起来像您的假设,即文件中的一行始终包含三列并不适用于所有行。用以下行替换 for 循环语句以消除异常并查看它发生的原因:

for (int x=0; x<result.length; x++)

回答by Rao's

This is the answer for above Question

这是上述问题的答案

 public class Readline {

/**
 * @param args
 */
public static void main(String[] args) {
    String fileName = "C:/Users/karthikrao/Desktop/cvsFile.csv";
    ArrayList<Integer> margins = new ArrayList<Integer>();
    BufferedReader br;
    String line, token;
    int i;
    try {
        br = new BufferedReader(new FileReader(fileName));
        try {
            while ((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line, ",\"");
                i = 0;
                while (st.hasMoreTokens()) {
                    token = st.nextToken();
                    if (margins.size() <= i) {
                        margins.add((Integer) token.length());
                    } else {
                        margins.set(
                                i,
                                Math.max(margins.get(i),
                                        (Integer) token.length()));
                    }
                    i++;
                }
            }

            br = new BufferedReader(new FileReader(fileName));
            while ((line = br.readLine()) != null) {
                StringTokenizer st = new StringTokenizer(line, ",\"");
                i = 0;
                while (st.hasMoreTokens()) {
                    token = st.nextToken();
                    System.out.print(token);
                    for (int j = 0; j < margins.get(i) - token.length(); j++) {
                        System.out.print(" ");
                    }
                    System.out.print("|");
                    i++;
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

}

}

回答by xiaolei yu

I suggest you not re-inventing wheel when there are so many great libraries out there. Try the uniVocity-parserswith the following code snippt as reference:

我建议你不要在有这么多伟大的图书馆时重新发明轮子。尝试使用以下代码片段作为参考的uniVocity 解析器

public static void main(String[] args) throws FileNotFoundException {

    /**
     * ---------------------------------------
     * Read CSV rows into 2-dimensional array
     * ---------------------------------------
     */

    // 1st, creates a CSV parser with the configs
    CsvParser parser = new CsvParser(new CsvParserSettings());

    // 2nd, parses all rows from the CSV file into a 2-dimensional array
    List<String[]> resolvedData = parser.parseAll(new FileReader("/examples/example.csv"));

    // 3rd, process the 2-dimensional array with business logic
    // ......
}

As you can see, only 2 lines required to finish the task of parsing csv data into array. Additionally, the library provides full list of features in parsing CSV data with excellent performance.

如您所见,只需 2 行即可完成将 csv 数据解析为数组的任务。此外,该库还提供了以出色性能解析 CSV 数据的完整功能列表。