2

While reading a CSV file, I need to ignore the first line. First row has date and heading of the CSV which I don't need to read.

I have to read only from the second row onward. Can anyone please help me?

String csvFilename = "C:\\Data\\csv_files\\REPORT.csv";
String filterCSV = "C:\\Data\\csv_files\\Output.csv";
CSVWriter write = new CSVWriter(new FileWriter(filterCSV));
CSVReader csvR = new CSVReader(new FileReader(csvFilename)); ---- 
List<CSVData> list = csv.parse(col, csvR);

for (Object object : list) {
    ------
}
0

6 Answers 6

3

Pretty simple:

 List<CSVData> list = csv.parse(col, csvR);
 list.remove(0);
Sign up to request clarification or add additional context in comments.

Comments

2

If you didnt write the CSVReader class by yourself then this constructor will skip the first line of the file:

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

2 Comments

I will try these options and let you know the results. thanks for the help
CSVReader reader = new CSVReader(new FileReader(file), ',', '\'', 1); - this worked as expected. Can you please explain what is '\'' means? ================================================================= I have also tried to use list.remove(0). But it gives empty csv as output. for (Object object : list) { CSVData csvdataBean = (CSVData) object; System.out.println("inside List object" + csvdataBean.getTransferDate()); =----- It returns null value when I use list.remove(0) } Thanks
2

Before reading the entire data in a while loop using BufferedReader use br.readLine() then first line of csv file will be eliminated, then read the remaining contents using readLine() function.

    BufferedReader br = new BufferedReader(new FileReader("file.csv"));
    String line = " ";
    br.readLine();
    while((line=br.readLine())!=null)
    {
        System.out.println(line);
    }

Comments

0

You can also use the methods in CSVParser.java

BufferedReader br = new BufferedReader(new FileReader("file.csv"));
CSVParser csvParser = new CSVParser(reader, 
CSVFormat.DEFAULT.withHeader("firstColHeader","secondColHeader").withFirstRecordAsHeader().withTrim());

Comments

0

You can use skip(int numberOfLinesToSkip) method from CSVReader class.

users.csv:

firstName,lastName
Edward,Norton
Brad,Pitt

My Spring Boot example:

implementation "com.opencsv:opencsv:$openCsvVersion"

import com.opencsv.CSVReader;

    @Value("${users.csv.file.name}")
    private String usersCsvFileName;

    public void fillUsersFromCsv() {
        List<User> users = new ArrayList<>();

        var resource = resourceLoader.getResource("classpath:csv/" + usersCsvFileName);

        try (var inputStream = resource.getInputStream();
             var inputStreamReader = new InputStreamReader(inputStream);
             var csvReader = new CSVReader(inputStreamReader)) {
            csvReader.skip(1);

            String[] nextRecord;
            while ((nextRecord = csvReader.readNext()) != null) {
                var firstName = nextRecord[0];
                var lastName = nextRecord[1];

                var user = User.builder()
                    .firstName(firstName)
                    .lastName(lastName)
                    .build();
                users.add(user);
            }

        } catch (Exception e) {
            log.error("Failed to read users from usersCsvFileName file: {}", e.getMessage());
        }

        userRepository.saveAll(users);
        log.error("{} users from usersCsvFileName successfully saved to DB", users.size());
    }

Comments

-1

First, you have to add into List like ArrayList and then remove index[0] like this:

list.remove(0);

1 Comment

Welcome to StackOverflow! This answer is just repeating the same thing that Pete B.'s answer above says.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.