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