To reading the data from csv file either use java IO library or usr third pary jar like "opencsv.jar". In given example I'm using opencsv.jar.
Step 1- Create csv file. "worldcup.csv"
1975,WestInddies,Australia,17 Run
1979,WestInddies,England,92 Run
1983,India,WestInddies,43 Run
1987,Australia,England,7 Run
1992,Pakistan,England,22 Run
1996,Sri Lanka,Australia,7 Wicket
1999,Australia,Pakistan,8 Wicket
2003,Australia,India,125 Run
2007,Australia,Sri Lanka,53 Run
2011,India,Sri Lanka,6 Wicket
2015,Australia,New Zeland,7 Wicket
Step 2- Create CSVReader object.
String csvFilename = "D:\\Ashish\\Personal\\WebApplication1\\worldcup.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
Step 3- Call csvReader.readNext() method to read data line by line
Example:
CSVReadWrite.java
Output:
Read Line by Line Example
====================================================
Year Winner Against By
1975 WestInddies Australia 17 Run
1979 WestInddies England 92 Run
1983 India WestInddies 43 Run
1987 Australia England 7 Run
1992 Pakistan England 22 Run
1996 Sri Lanka Australia 7 Wicket
1999 Australia Pakistan 8 Wicket
2003 Australia India 125 Run
2007 Australia Sri Lanka 53 Run
2011 India Sri Lanka 6 Wicket
2015 Australia New Zeland 7 Wicket
====================================================
Step 1- Create csv file. "worldcup.csv"
1975,WestInddies,Australia,17 Run
1979,WestInddies,England,92 Run
1983,India,WestInddies,43 Run
1987,Australia,England,7 Run
1992,Pakistan,England,22 Run
1996,Sri Lanka,Australia,7 Wicket
1999,Australia,Pakistan,8 Wicket
2003,Australia,India,125 Run
2007,Australia,Sri Lanka,53 Run
2011,India,Sri Lanka,6 Wicket
2015,Australia,New Zeland,7 Wicket
Step 2- Create CSVReader object.
String csvFilename = "D:\\Ashish\\Personal\\WebApplication1\\worldcup.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
Step 3- Call csvReader.readNext() method to read data line by line
Example:
CSVReadWrite.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | package com.atozjavatutorials; import com.opencsv.CSVReader; import com.opencsv.CSVWriter; import com.opencsv.bean.ColumnPositionMappingStrategy; import com.opencsv.bean.CsvToBean; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class CSVReadWrite { public static void main(String[] args) { try { readLineByLineExample(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static void readLineByLineExample() throws IOException { String csvFilename = "D:\\Ashish\\Personal\\WebApplication1\\worldcup.csv"; CSVReader csvReader = new CSVReader(new FileReader(csvFilename)); String[] row = null; System.out.println("Read Line by Line Example"); System.out.println("===================================================="); System.out.println("Year\tWinner\t\tAgainst\tBy"); while ((row = csvReader.readNext()) != null) { System.out.println(row[0] + "\t" + row[1] + "\t" + row[2]+ "\t" + row[3]); } System.out.println("===================================================="); csvReader.close(); } } |
Output:
Read Line by Line Example
====================================================
Year Winner Against By
1975 WestInddies Australia 17 Run
1979 WestInddies England 92 Run
1983 India WestInddies 43 Run
1987 Australia England 7 Run
1992 Pakistan England 22 Run
1996 Sri Lanka Australia 7 Wicket
1999 Australia Pakistan 8 Wicket
2003 Australia India 125 Run
2007 Australia Sri Lanka 53 Run
2011 India Sri Lanka 6 Wicket
2015 Australia New Zeland 7 Wicket
====================================================
No comments :
Post a Comment