Java opencsv-use Spring's PostConstruct mechanism to read the CSV configuration file to

Source: Internet
Author: User


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





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.