如何阅读 |在 java 中分隔 csv 文件?

How to read | separated csv file in java?

我有馅饼|分离的 csv 文件。我想在 java 中阅读此文件。我有一个 java 代码,它读取 java 中的逗号分隔文件,但它在 | 处失败分离的 csv 文件

我的文件包含 "CUSTOMER CODE | PRODUCT CODE | SEND TO BANK | SEND TO BRANCH" 此类数据不是逗号分隔的。


Java代码

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
public class TestPieCSVtoXLs {
    public static void main(String[] args) {
        String csvFile = "D:\VijayTest\csvFile.csv";
        DataInputStream myInput;
        String thisLine;
        ArrayList<ArrayList<String>> arList = new ArrayList<ArrayList<String>>();
        ArrayList<String> al = new ArrayList<String>();
        BufferedReader br = null;
        try {
            FileInputStream fis = new FileInputStream(csvFile);
            myInput = new DataInputStream(fis);
            while ((thisLine = myInput.readLine()) != null) {
                al = new ArrayList<String>();
                String strar[] = thisLine.split(",");
                for (int j = 0; j < strar.length; j++) {
                    System.out.println(strar[j]);
                    al.add(strar[j]);
                }
            }
            arList.add(al);
        } 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");
    }
}

你应该改变

String strar[] = thisLine.split(",");

String strar[] = thisLine.split("\|");

由于 | 是正则表达式中的元字符,您需要对其进行转义。 同样如评论中所述,此解决方案未考虑特殊情况...

OpenCSV 是一个很好的库,可以让您在 java.

中与 CSV 交互时变得轻松

阅读代码 | java

中的分隔 csv 文件
public static void main(String[] args) {
        String csvFile = "D:\VijayTest\csvfile.csv";
        DataInputStream myInput;
        String thisLine;
        ArrayList<ArrayList<String>> arList = new ArrayList<ArrayList<String>>();
        ArrayList<String> al = new ArrayList<String>();
        BufferedReader br = null;
        try {
            FileInputStream fis = new FileInputStream(csvFile);
            myInput = new DataInputStream(fis);
            while ((thisLine = myInput.readLine()) != null) {
                al = new ArrayList<String>();
                String strar[] = thisLine.split("\|");
                for (int j = 0; j < strar.length; j++) {
                    System.out.println(strar[j]);
                    al.add(strar[j]);
                }
            }
            arList.add(al);
        } 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");
    }

使用uniVocity-parsers并完成它:

    CsvParserSettings settings = new CsvParserSettings();
    settings.setLineSeparatorDetectionEnabled(true);
    settings.getFormat().setDelimiter('|');

    CsvParser parser = new CsvParser(settings);

    List<String[]> allLines = parser.parseAll(YOUR_INPUT_HERE);

它的 CSV 解析器可以处理各种输入,也是 Java 最快的 CSV 解析器。

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