Subscribe to Get Free Material Updates!
Visit my new blog WebData Scraping - Web Scraping Service provider company in India.

How To Create CSV File In Java


TestCsv.csv (Excel file) Looks as bellow:


/* Read Data From Comma Seprated File (CSV) using  FileInputStream and DataInputStream*/
import java.io.*;
import java.util.*;

class ReadCsv
{

 public static void main(String args[])
 {


  try
  {
  
  int sum=0;
  FileInputStream fi1=new FileInputStream("TestCsv.csv");
  DataInputStream di=new DataInputStream(fi1);


  String line;
   
   while((line=di.readLine())!=null)

   {

   StringTokenizer st=new StringTokenizer(line,",");

   while(st.hasMoreTokens())
   {
    String s=st.nextToken();
    System.out.println(s);
    sum=sum+Integer.parseInt(s);
   }
   
   
    System.out.println("Total:"+sum+"");
   }
   
  }

  catch(Exception e)
  {
   System.out.println("Error:"+e);
  }  

 }



}
Output:

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes