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: 
Please consider the use of [Commons CSV](http://commons.apache.org/csv/). This library is written according [RFC 4180 - Common Format and MIME Type for Comma-Separated Values (CSV) Files](https://www.rfc-editor.org/rfc/rfc4180). What is compatible to read such lines:

```
"aa,a","b""bb","ccc"

```

And the use is quite simple, there is just 3 classes, and a small sample according documentation:

> 
> Parsing of a csv-string having tabs as separators, '"' as an optional
> value encapsulator, and comments starting with '#':
> 
> 
> 

```
 CSVFormat format = new CSVFormat('\t', '"', '#');
 Reader in = new StringReader("a\tb\nc\td");
 String[][] records = new CSVParser(in, format).getRecords();

```

And additionally you get this parsers already available as constants:

* **DEFAULT** - Standard comma separated format as defined by RFC 4180.
* **EXCEL** - Excel file format (using a comma as the value delimiter).
* **MYSQL** - Default MySQL format used by the SELECT INTO OUTFILE and LOAD DATA INFILE operations.
TDF - Tabulation delimited format.
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.