How to read from a .csv file

Posted on 2006-05-26
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);

                    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

    Author Comment

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

    Accepted Solution

    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 ( )

    3. Comma Separated Values (CSV) - com.Ostermiller.util Java Utilities ( )

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

    LVL 10

    Expert Comment

    Hi royalcyber,

    Forgot to provide the link for CSV Parser - its

    LVL 9

    Expert Comment

    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 and C#.Net (which is just Java in a poor disguise), check out this question for details on my VB.NET answer..

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    fizzArray2 challenge 1 44
    fizzArray3  challenge 1 48
    noX challenge 17 55
    Microsoft C++ code failing in executable that worked 9 22
    RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now