If you want to read the CSV file and store the data into java object no need to set the data into java object one by one. Opencsv has given a nice feature for this.
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 pojo with appropriate member. In my case I have created Country.java with 4 member-
private String years;
private String winnerCountryName;
private String looserCountryName;
private String winningDetails;
Step 3- Create ColumnPositionMappingStrategy class object and call setType() method of this class by passing Country.class parameter
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(Country.class);
Step 4- Create String array as member name of pojo class and set this Strign array into strat object.
String[] columns = new String[]{"years", "winnerCountryName", "looserCountryName", "winningDetails"};
strat.setColumnMapping(columns);
Step 5- Create CsvToBean class object.
CsvToBean csv = new CsvToBean();
Step 6- Create CSVReader class object.
String csvFilename = "D:\\Ashish\\Personal\\WebApplication1\\worldcup.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
Step 7- Call csv.parse(strat, csvReader) method and get List object with all the data.
Step 8- Iterate this list and get Country class object
Country country = (Country) object;
Step 9- Call appropriate getter method of country class and get the data.
Example:
CSVReadWrite.java
Output:
[1975]:[WestInddies] Win againest [Australia] by [17 Run]
[1979]:[WestInddies] Win againest [England] by [92 Run]
[1983]:[India] Win againest [WestInddies] by [43 Run]
[1987]:[Australia] Win againest [England] by [7 Run]
[1992]:[Pakistan] Win againest [England] by [22 Run]
[1996]:[Sri Lanka] Win againest [Australia] by [7 Wicket]
[1999]:[Australia] Win againest [Pakistan] by [8 Wicket]
[2003]:[Australia] Win againest [India] by [125 Run]
[2007]:[Australia] Win againest [Sri Lanka] by [53 Run]
[2011]:[India] Win againest [Sri Lanka] by [6 Wicket]
[2015]:[Australia] Win againest [New Zeland] by [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 pojo with appropriate member. In my case I have created Country.java with 4 member-
private String years;
private String winnerCountryName;
private String looserCountryName;
private String winningDetails;
Step 3- Create ColumnPositionMappingStrategy class object and call setType() method of this class by passing Country.class parameter
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(Country.class);
Step 4- Create String array as member name of pojo class and set this Strign array into strat object.
String[] columns = new String[]{"years", "winnerCountryName", "looserCountryName", "winningDetails"};
strat.setColumnMapping(columns);
Step 5- Create CsvToBean class object.
CsvToBean csv = new CsvToBean();
Step 6- Create CSVReader class object.
String csvFilename = "D:\\Ashish\\Personal\\WebApplication1\\worldcup.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
Step 7- Call csv.parse(strat, csvReader) method and get List object with all the data.
Step 8- Iterate this list and get Country class object
Country country = (Country) object;
Step 9- Call appropriate getter method of country class and get the data.
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 38 39 | 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 { mapJavaBeanExample(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void mapJavaBeanExample() throws FileNotFoundException { ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy(); strat.setType(Country.class); String[] columns = new String[]{"years", "winnerCountryName", "looserCountryName", "winningDetails"}; strat.setColumnMapping(columns); CsvToBean csv = new CsvToBean(); String csvFilename = "D:\\Ashish\\Personal\\WebApplication1\\worldcup.csv"; CSVReader csvReader = new CSVReader(new FileReader(csvFilename)); List list = csv.parse(strat, csvReader); for (Object object : list) { Country country = (Country) object; System.out.print("["+country.getYears() + "]:[" + country.getWinnerCountryName() + "] Win againest ["); System.out.print(country.getLooserCountryName() + "] by ["); System.out.println(country.getWinningDetails() + "]"); } } } |
Output:
[1975]:[WestInddies] Win againest [Australia] by [17 Run]
[1979]:[WestInddies] Win againest [England] by [92 Run]
[1983]:[India] Win againest [WestInddies] by [43 Run]
[1987]:[Australia] Win againest [England] by [7 Run]
[1992]:[Pakistan] Win againest [England] by [22 Run]
[1996]:[Sri Lanka] Win againest [Australia] by [7 Wicket]
[1999]:[Australia] Win againest [Pakistan] by [8 Wicket]
[2003]:[Australia] Win againest [India] by [125 Run]
[2007]:[Australia] Win againest [Sri Lanka] by [53 Run]
[2011]:[India] Win againest [Sri Lanka] by [6 Wicket]
[2015]:[Australia] Win againest [New Zeland] by [7 Wicket]
No comments :
Post a Comment