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

标签 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:

“2012年3月” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” “2012/3” 无效的 构建成功(总时间:0 秒)

最佳答案

尝试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

关于java - 操作 CSV 文件中的数据时出现问题 (JAVA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28917291/

相关文章:

java - 如何使用java将阿拉伯语单词插入mysql数据库

c# - 简单的csv阅读器?

linux - 在 Linux 中打印 CSV 文件中的一列

csv - ColdFusion OpenCSV 打开内存中的文件

java - Java POI可以将图像写入word文档吗?

java - 命令行参数的 ArrayIndexOutOfBoundsException

java - GL_POLYGONMODE不知道如何正确使用

Java TTS(文本到语音)speech.properties 文件

php - CSS Graph- 条形图显示不正确

java - Guava 表到 CSV