操纵CSV文件(JAVA)中的数据时出现问题

时间:2015-03-07 16:45:34

标签: java csv

这可能听起来很愚蠢。我正在尝试从CSV文件中读取和处理数据。我将第一行保存为字符串数组(诸如年,月,日,名字,第二名,出生日期,国籍等)。好吧例如,我只需要年份,出生日期和名字。由于我有许多CSV文件,并且标题的顺序(第一行)正在改变,我必须将一些变量与年份的位置,出生日期和数组中的名字相关联。所以我尝试了很多可能性。其中一个在这里:

    int indexYear = 0;
    int indexMonth = 0;
    int indexDay = 0;
    int indexFirstname = 0;
    int indexSecondname = 0;
    String strForFirstLine="";

    strForFirstLine += input.readLine();
    String getFirstLine[] = strForFirstLine.split(",");
    for(int i=0; i<getFirstLine.length; ++i){
        if(getFirstLine[i].equals("'Year'"))
            indexYear = i;
        if(getFirstLine[i].equals("'Month'"))
            indexMonth = i;
        if(getFirstLine[i].equals("'Day'"))
            indexDay = i;
        if(getFirstLine[i].equals("'Firstname'"))
            indexFirstName = i;
        if(getFirstLine[i].equals("'Secondname'"))
            indexSecondName = i;
    }

提前致谢:)。

从arrayList获取SecondName的输出:

The output for getting the firstName: 'code' run:

&#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; &#34;3分之2012&#34; 空值 建立成功(总时间:0秒)

2 个答案:

答案 0 :(得分:2)

尝试CSVReader Api,这会让您的生活更轻松

实施例

CsvReader products = new CsvReader("products.csv");

            products.readHeaders();

            while (products.readRecord())
            {
                String productID = products.get("ProductID");
                String productName = products.get("ProductName");
                String supplierID = products.get("SupplierID");
                String categoryID = products.get("CategoryID");
                String quantityPerUnit = products.get("QuantityPerUnit");
                String unitPrice = products.get("UnitPrice");
                String unitsInStock = products.get("UnitsInStock");
                String unitsOnOrder = products.get("UnitsOnOrder");
                String reorderLevel = products.get("ReorderLevel");
                String discontinued = products.get("Discontinued");

                // perform program logic here
                System.out.println(productID + ":" + productName);
            }

            products.close();

然后在ArrayList

中添加要添加的内容

来源:CSV Reader In Java

答案 1 :(得分:0)

使用uniVocity-parsers CsvParser:

CsvParserSettings settings = new CsvParserSettings();
//defines the order of the fields you are interested in reading.
parserSettings.selectFields("ProductID", "ProductName", etc...);

CsvParser parser = new CsvParser(settings);
//returns the rows at the specified order
List<String[]> rows = parser.parseAll(new FileReader("your_input_file"));

如何订购每个CSV输入的标题并不重要。解析器将从输入中获取正确的值。如果CSV没有您选择的标题,则返回null。

查看文档,有很多选项可以帮助您处理各种野生输入。

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