且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用jsp / java从csv文件中的特定列读取数据?

更新时间:2021-12-20 09:32:00

我不认为你可以读取特定的列。使用 CSVParser 或您更好地阅读整行可以逐行读取CSV并拆分并获取 String 数组,那么您可以获取特定列,但是您需要读取整行增益。

I don't think you can read specific column.Better to read entire row using CSVParser or you can read CSV line by line and split it and get String array then you can get specific column but yes you need to read whole row gain.

尝试一下。

    String fName = "C:\\Amit\\abc.csv";
    String thisLine;
    int count = 0;
    FileInputStream fis = new FileInputStream(fName);
    DataInputStream myInput = new DataInputStream(fis);
    int i = 0;
    while ((thisLine = myInput.readLine()) != null) {
        String strar[] = thisLine.split(",");
            System.out.println(strar[3]);
                        // Here column 2
        }
    }

方式你可以读取特定的列。

By this way you can read specific column.