How to read from a .csv file

Posted on 2006-05-26
Medium Priority
Last Modified: 2007-12-19
I have an application where I used Jakarta POI-HSSF API to read data from an excel .xls file. I pass the fileInputStream to the method and it gives me an arraylist of rocords. One record contains an arraylist of cells. Now instead of reading daat from a .xls file, I need t read data from a .csv file, but I am not sure how to read data from a .csv file. I would greatly appreciate your help

here is my method which reads data from .xls file


  public static ArrayList xlsTextStripper(FileInputStream fis) {

        ArrayList record = new ArrayList();
        ArrayList document = new ArrayList();
        System.out.println(" I am in the xlsTextStripper method");
        try {

            HSSFWorkbook workbook = new HSSFWorkbook(fis);

            int numOfSheets = workbook.getNumberOfSheets();

            for (int i = 0; i < numOfSheets; i++) {
                System.out.println("The number os sheets are" + numOfSheets);

                HSSFSheet sheet = workbook.getSheetAt(i);

                Iterator rowIterator = sheet.rowIterator();
                int first_row = 0;
                while (rowIterator.hasNext()) {
                    if(first_row == 0){

                    HSSFRow row = (HSSFRow) rowIterator.next();

                    for (short input = row.getFirstCellNum();
                                       input <= (row.getLastCellNum() - 1);
                                       input++) {
                        HSSFCell cell = row.getCell(input);
                        String cellStringValue = null;
                        if (cell != null) {

                            if (cell.getCellType() == 4) {

                                boolean booleanValue = cell.getBooleanCellValue();

                                cellStringValue = Boolean.toString(booleanValue);

                            } else if (cell.getCellType() == 0) {

                                double doubleValue = cell.getNumericCellValue();

                                cellStringValue = Double.toString(doubleValue);

                            } else if (cell.getCellType() == 1) {

                                cellStringValue = cell.getStringCellValue();
                        } else {
                            cellStringValue = "";
                    System.out.println("First record added to the ArrayList");



    catch (Exception e) {



    return document;

Question by:royalcyber
  • 2

Author Comment

ID: 16772065
By the way I am using java, if you haven't figured it out yet :)
LVL 10

Accepted Solution

jagadeesh_motamarri earned 2000 total points
ID: 16773198
Hi royalcyber,

I would suggest you to use one of the following -

1. CSV Parser — The Java version is free and being merged with Java CSV Library

2. CSVReader and CSVWriter ( http://mindprod.com/jgloss/csv.html )

3. Comma Separated Values (CSV) - com.Ostermiller.util Java Utilities ( http://ostermiller.org/utils/CSV.html )

Please make sure you refer their examples which may be of grt8 help.

LVL 10

Expert Comment

ID: 16773203
Hi royalcyber,

Forgot to provide the link for CSV Parser - its http://www.csvreader.com/


Expert Comment

ID: 16788689
in the interest of keeping it simple, just like to remind you of this....

Header1 , Header2  (NEWLINE)
Value1a , Value2a (NEWLINE)
Value1b , Value2B (NEWLINE)

That is a CSV file and is inherently LESS complicated than reading from a worksheet! All you need to do is read each line (i.e. split the file by 'NEWLINE' (chr(10) & Chr(13)) then split each read line by Comma.

This is dead easy in VB.net and C#.Net (which is just Java in a poor disguise), check out this question for details on my VB.NET answer..


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Introduction to Processes

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question