How to skip first line of a CSV file in Java?

  • Java
  • 4 mins read

Introduction

CSV (Comma Separated Values) files are a common file format for storing and exchanging data. In this tutorial, we will learn how to skip the first line of a CSV file while reading it in Java. This can be useful when the first line of a CSV file contains the headers and we don't want to include them in our data.

Skip first line of a CSV file in Java Example

Step 1: Import the necessary libraries In order to read a CSV file in Java, we will need to import the following libraries:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

Step 2: Read the CSV file We will use the BufferedReader class to read the CSV file. The code to read the file would look something like this:

BufferedReader reader = new BufferedReader(new FileReader("file.csv"));
String line;
while ((line = reader.readLine()) != null) {
    // code to process the line
}
reader.close();

Step 3: Skip the first line To skip the first line of the CSV file, we can add an if statement before the while loop that checks if the current line is the first line. If it is, we can simply call the readLine() method again to move to the next line. The modified code would look something like this:

BufferedReader reader = new BufferedReader(new FileReader("file.csv"));
String line = reader.readLine(); // read the first line
if (line != null) {
    while ((line = reader.readLine()) != null) {
        // code to process the line
    }
}
reader.close();

Step 4: Split the line We can use the split() method to split the line into fields using a delimiter (e.g. "," or ";"). Here's an example of how to split the line by a comma:

String[] fields = line.split(",");

Step 5: Process the fields Now we can access the individual fields of the line and process them as needed. Here's an example of how to print the fields:

for (String field : fields) {
    System.out.println(field);
}

Complete Example

Here's the complete example of reading a CSV file and skipping the first line:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class CSVReader {
    public static void main(String[] args) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("file.csv"));
            String line = reader.readLine(); // read the first line
            if (line != null) {
                while ((line = reader.readLine()) != null) {
                    String[] fields = line.split(",");
                    for (String field : fields) {
                        System.out.println(field);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

This example uses a try-catch-finally block to handle any IOExceptions that may occur while reading the file. The BufferedReader is closed in the finally block to ensure that it is always closed, even if an exception is thrown.

Note that this example simply prints out each field of the CSV file, you can replace the System.out.println(field); with your own processing code.

Also, change the file name to match the correct file name you want to read.

Conclusion

In conclusion, this tutorial provided a step-by-step guide on how to skip the first line of a CSV file while reading it in Java. We learned how to import the necessary libraries, read the CSV file using the BufferedReader class, skip the first line by calling the readLine() method before the while loop, split the line into fields using the split() method, and process the fields as needed.

With this knowledge, you can now read CSV files in Java and skip the first line if it contains headers or any other unwanted data. Remember to use the try-catch-finally block to handle any IOExceptions that may occur while reading the file, and close the BufferedReader in the finally block to ensure that it is always closed.

Related: