Dinesh Santhalingam

Dinesh Santhalingam

  • NA
  • 737
  • 368.4k

Read column wise in csv file using java

Mar 29 2017 12:32 AM
I have a csv file.I want to read my csv file and store it in a seperate array using java.
 ID Name
 1 ramesh
 2 rani
 3 rahane
 4 raja
 
  1.     public static void main(String[] args) throws IOException {  
  2.    ArrayList<String> ar = new ArrayList<String>();    
  3.  File csvFile = new File("C:\\Desktop\\r2.csv");  
  4.          BufferedReader br = new BufferedReader(new FileReader(csvFile));  
  5.          String line = "";  
  6.          StringTokenizer st = null;  
  7.          int lineNumber = 0;   
  8.          int tokenNumber = 0;  
  9.          while ((line = br.readLine()) != null) {  
  10.            lineNumber++;  
  11.            //use comma as token separator  
  12.            st = new StringTokenizer(line, ",");  
  13.            while (st.hasMoreTokens()) {  
  14.              tokenNumber++;  
  15.               String sd=st.nextToken() + "  ";  
  16.                 
  17.              if(sd!=null){  
  18.              ar.add(sd);  
  19.                  System.err.println(sd);  
  20.              }  
  21.                
  22.            }  
  23.            tokenNumber = 0;  
  24.          }  
  25.  
 In this Method all element stores in a single arraylist.
  1. O/p:  
  2. ID  
  3. Name  
  4. 1  
  5. ramesh  
  6. 2  
  7. rani  
  8. 3  
  9. rahane  
  10. 4  
  11. raja  
But my I want output like this:
  1. o/p  
  2. ID     Name  
  3. 1      ramesh  
  4. 2      rani
  5. 3      rahane
  6. 4      raja
  two separate object.
 help me to olve my issue.

Answers (1)