Java opencsv-use Spring's PostConstruct mechanism to read the CSV configuration file to the memory map at project startup
A. studentconfig.csv file:
ID, NAME, SCORE1, Zhang San, 1002, Li Si, 903, Zhao Wu, 604, Wang Liu, 55
Ii. StudentConfigBean. java file:
Package com. jiangge. config; public class StudentConfigBean {/*** configure Bean */private int id; private String name; private int score; public int getId () {return id ;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getScore () {return score;} public void setScore (int score) {this. score = score ;}}
Iii. StudentConfig. java file:
Package com. jiangge. config; import java. io. dataInputStream; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. inputStreamReader; import java. io. unsupportedEncodingException; import java. util. hashMap; import java. util. list; import java. util. map; import org. springframework. stereotype. component; import au.com. bytecode. opencsv. CSVReader; import au.com. bytecode. opencsv. bean. columnPositionMappingStrategy; import au.com. bytecode. opencsv. bean. csvToBean; import au.com. bytecode. opencsv. bean. headerColumnNameTranslateMappingStrategy; import javax. annotation. postConstruct; @ Componentpublic class StudentConfig {/*** use Spring's PostConstruct mechanism to read the CSV configuration file to the memory map at project startup * library version: opencsv-2.3.jar */public static Map
StudentMap; @ PostConstruct public void init () {HeaderColumnNameTranslateMappingStrategy
Strategy = new HeaderColumnNameTranslateMappingStrategy
(); Strategy. setType (StudentConfigBean. class); Map
ColumnMapping = new HashMap
(); ColumnMapping. put ("ID", "id"); // columing columnMapping between the csv header and java class member variables. put ("NAME", "name"); columnMapping. put ("SCORE", "score"); strategy. setColumnMapping (columnMapping); List
List = null; String csvFilename = "C: \ studentConfig.csv"; // place TODO in the project path CSVReader csvReader = null; DataInputStream in = null; try {in = new DataInputStream (new FileInputStream (new File (csvFilename);} catch (FileNotFoundException e) {e. printStackTrace ();} try {csvReader = new CSVReader (new InputStreamReader (in, "UTF-8"); // solve Chinese garbled characters and pay attention to the csv file encoding format .} catch (UnsupportedEncodingException e) {e. printStackTrace ();} CsvToBean
CsvToBean = new CsvToBean
(); List = csvToBean. parse (strategy, csvReader); studentMap = new HashMap
(); For (Object object: list) {StudentConfigBean studentCfgBean = (StudentConfigBean) object; studentMap. put (studentCfgBean. getId (), studentCfgBean); // put it in map System. out. println ("Student configuration file read"); System. out. println ("Id =>" + studentCfgBean. getId () + "Name =>" + studentCfgBean. getName () + "Score =>" + studentCfgBean. getScore ());}}}
4. Output:
Student configuration file read Id => 1 Name => Zhang San Score => 100 student configuration file read Id => 2 Name => Li Si Score => 90 student configuration file read Id => 3 Name => Zhao wuscore => 60 student configuration file read Id => 4 Name => Wang liuscore => 55