简体   繁体   中英

Java: Using InputStream and Apache Commons CSV without line numbers

This is probably very simple, but I have not been able to find an option to do this. I'm trying to Apache Commons CSV to read a file for later validations. The CSV in question is submitted as an Input Stream, which seems to add an additional column to the file when it reads it, containing the line numbers. I would like to be able to ignore it, if possible, as the header row does not contain a number, which causes an error. Is there an option already in InputStream to do this, or will I have to set up some kind of post processing?

The code I'm using is as follows:

public String validateFile(InputStream filePath) throws Exception{
        System.out.println("Sending file to reader");
        System.out.println(filePath);
        InputStreamReader in = new InputStreamReader(filePath);
        //CSVFormat parse needs a reader object
        System.out.println("sending reader to CSV parse");
        for (CSVRecord record : CSVFormat.DEFAULT.withHeader().parse(in)) {
            for (String field : record) {
                System.out.print("\"" + field + "\", ");
            }
            System.out.println();
        }
        return null;
    }

When using withHeader() , I end up with the following error:

java.lang.IllegalArgumentException: A header name is missing in [, Employee_ID, Department, Email]

and I can't simply skip it, as I will need to do some validations on the header row.

Also, here is an example CSV file:

"Employee_ID", "Department", "Email"
"0123456","Department of Hello World","John.Doe@gmail.com"

EDIT: Also, The end goal is to validate the following:

  1. That there are columns called "Employee_ID", "Department", and "Email". For this, I think I'll need to remove.withHeader().
  2. Each line is comma delimited.
  3. There are no empty cells values

Newer versions of Commons-CSV have trouble with empty headers. Maybe that's the case here as well? You just mentioned "no empty cell values" not sure if this included headers as well...

Also see: https://issues.apache.org/jira/browse/CSV-257

Setting .setAllowMissingColumnNames(true) did the trick for me.

final CSVFormat csvFormat = CSVFormat.Builder.create()
        .setHeader(HEADERS)
        .setAllowMissingColumnNames(true)
        .build();
final Iterable<CSVRecord> records = csvFormat.parse(reader);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM