0

I use the following code to read a .csv file in Java. How can I print the header of the file using this code? (I use the packages edu.duke and org.apache.commons.csv from here.)

import edu.duke.*;
import org.apache.commons.csv.*;
import java.io.*;

public class myCSVParser {
    public static void readData() {
        FileResource fr = new FileResource("smauto2.csv");
        CSVParser ps = fr.getCSVParser();
        // ??
    }
    public static void main(String[] args) {
        readData();
    }
}

1 Answer 1

1

You can use this code

String fileName = "data.csv";
CSVReader reader = new CSVReader(new FileReader(fileName ));

// if the first line is the header

String[] header = reader.readNext();

OR

 BufferedReader br = new BufferedReader(new FileReader("myfile.csv"));
    String header = br.readLine();
    if (header != null) {
        String[] columns = header.split(",");
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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