java - 我想使用循环从 CSV 文件 java 中查找列的值

标签 java csv

请原谅我,如果这是一个基本(或没有很好解释)的问题,我对 Java 相当陌生,并且一直在阅读大量 Material 并试图理解相关的 Javadoc,但无济于事。

为了简要介绍我要创建的内容,我创建了一个阅读器类,它从 csv 文件(4 行长)读取数据,包括项目 ID、价格、描述等字段。我创建了一个单独的演示类,用于显示此 csv 文件的详细信息(通过创建阅读器类的实例),现在我尝试创建一个方法,要求用户输入项目 ID,然后根据用户输入的 ID 显示相应的项目。我所坚持的部分是访问 csv 文件中的特定行/列,然后将它们与给定字符串进行比较(由用户输入,对应于 csv 文件中的特定字段)

这是我迄今为止想到的:

input = new Scanner(System.in);
    System.out.println("Enter a product code");
    String prodC = input.next();
    //Here I want to know if there is a way of accessing a field in a csv file

任何想法将不胜感激。

更新

感谢您的快速回复,我目前正在通读并了解如何尝试实现各种技术。为了回应有关文件阅读器的评论,我是这样阐述的:

 public CatalogueReader(String filename) throws FileNotFoundException {
    this.filename = filename;
    this.catalogue = new Catalogue();

    Scanner csvFile;
    try {
        csvFile = new Scanner(new File(filename));
    } catch (FileNotFoundException fnf) {
        throw new FileNotFoundException("File has not been found!");
    }
    csvFile.useDelimiter("\n");
    boolean first = true;
    String productCode;
    double price;
    String description;
    double weight;
    int rating;
    String category;
    boolean ageRestriction;
    String csvRows;
    while (csvFile.hasNextLine()) {
        csvRows = csvFile.nextLine();
        if (first) {
            first = false;
            continue;
        }
        System.out.println(csvRows);
        String[] fields = csvRows.split(",");
        productCode = (fields[0].trim());
        price = Double.parseDouble(fields[1].trim());
        description = fields[2].trim();
        weight = Double.parseDouble(fields[3].trim());
        rating = Integer.parseInt(fields[4].trim());
        category = fields[5].trim();
        ageRestriction = Boolean.parseBoolean(fields[6].trim());
        catalogue.addAProduct(new Item(productCode, price, description, weight, rating, category, ageRestriction));             
    }
    csvFile.close();
}

}

最佳答案

好的,对于这样的 CSV 文件:

"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"

这里是如何使用 Java Native Libraries 进行阅读的示例

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CSVReader {

  public static void main(String[] args) {

    CSVReader obj = new CSVReader();
    obj.run();

  }

  public void run() {

    String csvFile = YOURFILEPATHHERE ;
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
            String[] country = line.split(cvsSplitBy);

            System.out.println("Country [code= " + country[4] 
                                 + " , name=" + country[5] + "]");

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    System.out.println("Done");
  }

}

这有帮助吗?

关于java - 我想使用循环从 CSV 文件 java 中查找列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20905509/

相关文章:

string - 如何在 Perl 中反转一串数字?

python - 使用 Python 将 .csv 文件转换为 .dbf?

Python pandas - 将目录中的csv文件合并为一个

swift - 用户可以从您的 Xcode 项目访问哪些文件/数据 - Swift 4

java - spring security 中 auto-config=true 有什么用

java - 使用 spring data findAll 从 MongoDB 中检索数据(Example<S> example)

java - 在TreeSet中查找相等的元素

java - 应用程序在启动前崩溃

java - 谷歌日历 v3 Java API : Insert event

c - 段错误,但找不到位置