I have been looking for the past 2 hours for a solution to my problem in vain. I'am trying to read a CSV File using Apache commons ,I am able to read the whole file but my problem is how to extract only the header of the CSV in an array?
Asked
Active
Viewed 2.7k times
6 Answers
9
I looked everywhere and even the solution above didn't work. For anyone else with this issue, this does.
Iterable<CSVRecord> records;
Reader in = new FileReader(fileLocation);
records = CSVFormat.EXCEL.withHeader().withSkipHeaderRecord(false).parse(in);
Set<String> headers = records.iterator().next().toMap().keySet();
Note that your use of .next()
has consumed one row of the CSV.
6
By default, first record read by CSVParser
will always be a header record, e.g. in the below example:
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING);
FileReader fileReader = new FileReader("file");
CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat);
List csvRecords = csvFileParser.getRecords();
csvRecords.get(0)
will return the header record.

Darshan Mehta
- 30,102
- 11
- 68
- 102
-
2This is an impossible answer. It appears to have been cribbed from https://examples.javacodegeeks.com/core-java/apache/commons/csv-commons/writeread-csv-files-with-apache-commons-csv-example/. Note that `FILE_HEADER_MAPPING` contains the header.. which is what the original question was asking for. – daveloyall Apr 06 '21 at 22:38
-
1As @daveloyall was pointing out, the answer does not address the original question as the code provided assumes that the header fields are already available in FILE_HEADER_MAPPING. I downvoted so better answers can bubble to the top. – Alex Voss Sep 10 '22 at 12:13
6
BufferedReader br = new BufferedReader(new FileReader(filename));
CSVParser parser = CSVParser.parse(br, CSVFormat.EXCEL.withFirstRecordAsHeader());
List<String> headers = parser.getHeaderNames();
This worked for me. The last line is what you need, extracts the headers found by the parser into a List of Strings.

Justin Geeslin
- 3,351
- 1
- 19
- 9
-
CSVFormat.EXCEL is not working for me at all. Can you take a look at this and help me. https://stackoverflow.com/questions/67964627/how-to-read-excel-xlsx-multipartfile-using-apache-csv-parser-current-approac @Justin – Deepanshu Rathi Jun 14 '21 at 07:09
6
Since Apache Commons CSV v1.9.0, the withSkipHeaderRecord()
& the withFirstRecordAsHeader()
methods are deprecated. A builder interface is provided. Use it thusly:
CSVFormat.DEFAULT.builder()
.setHeader()
.setSkipHeaderRecord(true)
.build();

Lavie Tobey
- 484
- 1
- 4
- 12
-
This is a good answer, but not complete. It sent me looking for more. I think you should add something like`Reader in = new FileReader(fileName, StandardCharsets.UTF_8);` and `CSVFormat cSVFormat = CSVFormat.DEFAULT.builder().setHeader().setSkipHeaderRecord(false).build();` and `Iterable
records = CSVParser.parse(in, cSVFormat);` – SedJ601 Apr 20 '23 at 15:43
1
In Kotlin:
val reader = File(path).bufferedReader()
val records = CSVFormat.DEFAULT.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withTrim()
.parse(reader)
println(records.headerNames)

Mitchell Tracy
- 1,395
- 1
- 15
- 16
0
The code below works for me:
import java.io.FileReader;
import org.apache.commons.csv.*;
public static String[] headersInCSVFile (String csvFilePath) throws IOException {
//reading file
CSVFormat csvFileFormat = CSVFormat.DEFAULT;
FileReader fileReader = new FileReader(csvFilePath);
CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat);
List csvRecords = csvFileParser.getRecords();
//Obtaining first record and splitting that into an array using delimiters and removing unnecessary text
String[] headers = csvRecords.get(0).toString().split("[,'=\\]\\[]+");
String[] result = new String[headers.length - 6];
for (int i = 6; i < headers.length; i++) {
//.replaceAll("\\s", "") removes spaces
result[i - 6] = headers[i].replaceAll("\\s", "");
}
return result;
}

Muaz
- 1
- 1