Wednesday, November 7

Reading CSV file using java

// This is code  read csv file columns and display as it is in console. this is not consider empty value

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;

public class csvRead {
        public static void main(String[] args) {            
                try
                {
                        String filePath = "C:/xls/csv_person.csv";   //csv file path
                        String str = "";
                        StringTokenizer st = null;
                        int lineNo = 0, tokenNo = 0;
                     
                        //create BufferedReader to read csv file
                        BufferedReader br = new BufferedReader( new FileReader(filePath));

                        //read comma separated file line by line
                             while( ( str =br.readLine()) != null) {                            
                                br.readLine();
                              
                               //break comma separated line using ","
                                st = new StringTokenizer(str, ",");
                                while(st.hasMoreTokens()) {
                                        tokenNo++;
                                        System.out.println( st.nextToken());
                                }
                              
                                //reset token number & increment the lineNo
                                 lineNo++;
                                tokenNo = 0;
                        }
                }
                catch(Exception e){
                        System.out.println("Exception while reading csv file: " + e);               
                }
        }
}

output:
~~~~~


USERNAME
CUSTOM1
CUSTOM2
CUSTOM3
TYPE
CREATED_ON
ALLOW_NONBUDDIES
CUSTOM5
String lenght46
20501427
vivek
hello
hai
2
01-jan-12
50
record
String lenght32
40021456
vivek
sample
record
String lenght0
String lenght0
String lenght0
String lenght46
20501427
vivek
hello
hai
2
01-jan-12
50
record
String lenght0
String lenght0
String lenght0
String lenght20
basker 65
vivek

1 comment:

Backup files to google drive using PHP coding

 Dear All, To backup files to Google Drive using PHP coding, you can use the Google Drive API and the Google Client Library for PHP. Here...