Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Read CSV data using Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 633
    Comment on it

    Hello

    In this blog I will give a demo about how to read CSV data from a file. For this purpose I will use Opencsv library. Opencsv  is a very simple csv (comma-separated values) parser library for Java.

    You can download  the binary and source jar file from here https://sourceforge.net/projects/opencsv/

    Here is a demo program to read the data of a csv file named as myCsv.csv

     

    Content of ReadCSV

    import java.io.FileReader;
    import java.io.IOException;
    
    import com.opencsv.CSVReader;
    
    public class ReadCSV {
    	public static void main(String[] args) throws IOException {
    	
    		CSVReader reader = new CSVReader(new FileReader("myCsv.csv"));
    		String[] nextLine;
    		while ((nextLine = reader.readNext()) != null) {
    			// nextLine[] is an array of values from the line
    			System.out.println(nextLine[0] + nextLine[1] + nextLine[2] + nextLine[3]);
    		}
    		reader.close();
    	}
    }

     

    The content of my csv file is shown below

     

    Run the ReadCsv program to read the content of CSV file. After running the above program the output will be like this.

    Company	Jan	Feb	Mar
    Bajaj	1234	3333	8643
    Honda	1000	6777	4566
    Yamaha	-1	4456	4564
    KTM	6789	3483	4999
    TVS	9981	4333	4446

     

    Thanks for reading :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: