Question: I'm working on a csv parser, I want to read headers and the rest of the csv file separately.
Here is my code to read csv. 

The current code reads everything in the csv file, but I need to read headers separate.
please help me regarding this.

```
public class csv {

private void csvRead(File file)
{
    try
    {
    BufferedReader br = new BufferedReader( new FileReader(file));
    String strLine = "";
    StringTokenizer st = null;
    File cfile=new File("csv.txt");
    BufferedWriter writer = new BufferedWriter(new FileWriter(cfile));
    int tokenNumber = 0;

    while( (strLine = br.readLine()) != null)
    {
            st = new StringTokenizer(strLine, ",");
            while(st.hasMoreTokens())
            {

                    tokenNumber++;
                    writer.write(tokenNumber+"  "+ st.nextToken());
                    writer.newLine();
            }

            tokenNumber = 0;
            writer.flush();
    }
}

    catch(Exception e)
    {
        e.getMessage();
    }
}

```

Answer: 
Have you considered [OpenCSV](http://opencsv.sourceforge.net/)?

Previous question here...

[CSV API for Java](https://stackoverflow.com/questions/101100/csv-api-for-java)

Looks like you can split out the header quite easily...

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

// if the first line is the header
String[] header = reader.readNext();

// iterate over reader.readNext until it returns null
String[] line = reader.readNext();

```
Your code here, being

```
while( (strLine = br.readLine()) != null)
    {
      //reads everything in your csv
    } 

```

will print all of your CSV content.

For example, the following fetches your header:

```
Reader in = ...;
 CSVFormat.EXCEL.withHeader("Col1", "Col2", "Col3").parse(in);

```

As suggested, life could be easier using the predefined CSVFormat from the apache commons library. Link here (<https://commons.apache.org/proper/commons-csv/user-guide.html>).
Cheers.