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 |
- public static void main(String[] args) throws IOException {
- ArrayList<String> ar = new ArrayList<String>();
- File csvFile = new File("C:\\Desktop\\r2.csv");
- BufferedReader br = new BufferedReader(new FileReader(csvFile));
- String line = "";
- StringTokenizer st = null;
- int lineNumber = 0;
- int tokenNumber = 0;
- while ((line = br.readLine()) != null) {
- lineNumber++;
- //use comma as token separator
- st = new StringTokenizer(line, ",");
- while (st.hasMoreTokens()) {
- tokenNumber++;
- String sd=st.nextToken() + " ";
-
- if(sd!=null){
- ar.add(sd);
- System.err.println(sd);
- }
-
- }
- tokenNumber = 0;
- }
- }
In this Method all element stores in a single arraylist.
- O/p:
- ID
- Name
- 1
- ramesh
- 2
- rani
- 3
- rahane
- 4
- raja
But my I want output like this:
- o/p
- ID Name
- 1 ramesh
- 2 rani
- 3 rahane
- 4 raja
two separate object.
help me to olve my issue.