How to Skip the First Line of a CSV in Java

Skip first line while reading CSV file in Java

You might consider placing headerLine = br.readLine() before your while loop so you consume the header separately from the rest of the file. Also you might consider using opencsv for csv parsing as it may simplify your logic.

How can I skip the first line of a csv in Java?

You may want to read the first line, before passing the reader to the CSVParser :

static void processFile(final File file) {
FileReader filereader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(filereader);
bufferedReader.readLine();// try-catch omitted
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
CSVParser parser = new CSVParser(bufferedReader, format);
final List<CSVRecord> records = parser.getRecords();
//stuff
}

Skipping first line in csv using linereader.readline() but facing code quality issue

Rather than doing an inline .readLine() to skip over an item, sonarqube is saying that you should still assign it to a variable.

"When a method is called that returns data read from some data source, that data should be stored rather than thrown away. Any other course of action is surely a bug." - Sonarqube docs

This is being done as arbitrarily throwing away data can be very dangerous on large scales where no one may be able to easily see the input/output of a specific file.

Basically rather than:

bufferedReader.readline()

String line

//do whatever file read you're going to do thing

You should have:

String line = bufferedReader.readline()

//Do whatever logic you're going to do with the file

This should satisfy Sonarqube.

Java Reading CSV file into array but ignore first line

Add a readLine() before the while loop to skip the first line.

br = new BufferedReader(new FileReader(file));
br.readLine(); //read the first line and throw it away
while ((line = br.readLine()) != null) {

Skip first line using Open CSV reader

This constructor of CSVReader class will skip 1st line of the csv while reading the file.

CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1);


Related Topics



Leave a reply



Submit