01 June 2012

Read CSV file using Java.

Using this code read .csv file, it's required  following jar.
           javacsv.jar



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;


public class ReadCsvFile {
                String[] strArray ;
                String  variable_1 =  "";
                String variable_2 =  "";
                String variable_2 =  "";
                String a3 = "";
                int number ;
                int count = 0;
                String CsvFilePath="";
                public void CsvFileParser(String CsvFile){
                                this.CsvFilePath = CsvFile;
                                String result="";                                               
                                try {
                                                CsvReader products = new CsvReader(CsvFilePath);                                      
                                                while(products.readRecord()){
                                                                count++;                                                                                                                                                                                                              result = products.getRawRecord();
                                                                strArray = result.split(",");
                                                                variable_1  = strArray[0].trim();
                                                                variable_2   = strArray[1].trim();
                                                                variable_3  = strArray[2].trim();
                                                               
                                               System.out.println("Variable 1 : "+variable_1);
                                               System.out.println("Variable 2 : "+variable_2);
                                               System.out.println("Variable 3 : "+variable_3);
                                                                }
                                                } catch (FileNotFoundException e) {                                                                                                                                                        e.printStackTrace();                                                       
                                                } catch (IOException e) {
                                                                e.printStackTrace();
                                                }
                }                             
                public static void main(String[] args) {
                                ReadCsvFile x = new ReadCsvFile();
                                String CsvFile = "C:/ License.csv";              //CSV file path
                                try {
                                                                x.CsvFileParser(CsvFile);                                                                                                              
                                                } catch (Exception e) {
                                                                e.printStackTrace();
                                                }
                 }
}
Using this code read .csv file, it's required  following jar.
           javacsv.jar



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;


public class ReadCsvFile {
                String[] strArray ;
                String  variable_1 =  "";
                String variable_2 =  "";
                String variable_2 =  "";
                String a3 = "";
                int number ;
                int count = 0;
                String CsvFilePath="";
                public void CsvFileParser(String CsvFile){
                                this.CsvFilePath = CsvFile;
                                String result="";                                               
                                try {
                                                CsvReader products = new CsvReader(CsvFilePath);                                      
                                                while(products.readRecord()){
                                                                count++;                                                                                                                                                                                                              result = products.getRawRecord();
                                                                strArray = result.split(",");
                                                                variable_1  = strArray[0].trim();
                                                                variable_2   = strArray[1].trim();
                                                                variable_3  = strArray[2].trim();
                                                               
                                               System.out.println("Variable 1 : "+variable_1);
                                               System.out.println("Variable 2 : "+variable_2);
                                               System.out.println("Variable 3 : "+variable_3);
                                                                }
                                                } catch (FileNotFoundException e) {                                                                                                                                                        e.printStackTrace();                                                       
                                                } catch (IOException e) {
                                                                e.printStackTrace();
                                                }
                }                             
                public static void main(String[] args) {
                                ReadCsvFile x = new ReadCsvFile();
                                String CsvFile = "C:/ License.csv";              //CSV file path
                                try {
                                                                x.CsvFileParser(CsvFile);                                                                                                              
                                                } catch (Exception e) {
                                                                e.printStackTrace();
                                                }
                 }
}

No comments:

Post a Comment

Thank you.