CSV¤òÆɤ߹þ¤àJava¥é¥¤¥Ö¥é¥ê †»Å»ö¤Ç¤Ï¤è¤¯CSV¥Õ¥¡¥¤¥ë¤òÆɤ߹þ¤ó¤Ç±¾¡¹¤È¤¤¤Ã¤¿¶È̳½èÍý¤¬·ë¹½É¬Íפˤʤ俤ꤷ¤Þ¤¹¤¬¡¢Java¤Î¥é¥¤¥Ö¥é¥ê¤òõ¤·¤Æ¤¤¤¿¤È¤³¤í¡¢¤è¤µ¤²¤Ê¥é¥¤¥Ö¥é¥ê¤¬¸«¤Ä¤«¤ê¤Þ¤·¤¿¡£opencsv - an open source csv parser for Java ¤Ç¤¹¡£ ¤Á¤ç¤Ã¤È¿¨¤Ã¤¿¤È¤³¤íÈó¾ï¤Ë¥·¥ó¥×¥ë¤Ç»È¤¤¤ä¤¹¤«¤Ã¤¿¤Î¤Ç¥á¥â¤Ã¤Æ¤ª¤¤Þ¤¹¡£ ¥À¥¦¥ó¥í¡¼¥É †opencsv - an open source csv parser for Java ¤è¤ê¡¢¥À¥¦¥ó¥í¡¼¥É²Äǽ¤Ç¤¹¡£ »È¤Ã¤Æ¤ß¤ë¡£ †Eclipse¤Î¥×¥í¥¸¥§¥¯¥È¤È¤·¤Æ¥¢¥Ã¥×¤·¤Þ¤·¤¿¡£ http://www.masatom.in/viewvc/trunk/CSVReaderSamples/?root=Examples¤è¤ê¥À¥¦¥ó¥í¡¼¥É²Äǽ¤Ç¤¹¡£ ¥µ¥ó¥×¥ë¤Ï°Ê²¼¤Î¤è¤¦¤Ê´¶¤¸¤Ç¤¹
¼Â¹Ô·ë²Ì¤Ï°Ê²¼¤ÎÄ̤ꡣ À« ̾ etc... ¤È¤¦¤¤ç¤¦ ¤¿¤í¤¦ etc... Åìµþ ÂÀϺ etc... csv¥Õ¥¡¥¤¥ë¤ò¥Ñ¡¼¥¹¤·¤Æ¡¢Å¬ÀÚ¤Ëʬ³ä¤·¤Æ¤¯¤ì¤Æ¤¤¤Þ¤¹¡£ ¥µ¥ó¥×¥ë¤Ïñ½ã¤Ç¤·¤¿¤¬¡¢¥À¥Ö¥ë¥¯¥©¡¼¥È("")¤Ç°Ï¤Þ¤ì¤¿CSV¤ä¥¿¥Ö¶èÀÚ¤ê¤Î¥Õ¥¡¥¤¥ë¡¢""Æâ¤Î¥«¥ó¥Þ¤Î¼è¤ê°·¤¤¡¢ÅÓÃæ¤Ë²þ¹Ô¤¬Æþ¤ëCSV¡¢¤¿¤È¤¨¤Ð À«,̾,ǯÎð ¤È¤¦¤¤ç¤¦,"¤¿ ¤í¤¦",33 Åìµþ,ÂÀϺ,15 ¤³¤ó¤Ê¤¿¤Á°¤¤CSV¤Ê¤É¤â¡¢Àµ¤·¤¯¼è¤ê°·¤¦¤³¤È¤¬¤Ç¤¤Þ¤¹¡£¤¹¤´¤¤¤Ç¤¹¡£¡£ JavaBeans?¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë †¤µ¤ÆÀè¤Î¤è¤¦¤Ë¡¢´Êñ¤ËCSV¤ò¥Ñ¡¼¥¹¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤·¤¿¤¬¡¢¤³¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤È¤Æ¤âÊØÍø¤Ê¤Î¤Ï¡¢CSV¥Ç¡¼¥¿¤ò»ØÄꤷ¤¿JavaBeans?¤ËľÀܥޥåԥ󥰤Ǥ¤ë¤È¤³¤í¤Ç¤¹¡£¤¿¤È¤¨¤Ð¤µ¤Ã¤¤Î¡¢
¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë»ö¤¬¤Ç¤¤ë¤ó¤Ç¤¹¤Í¡£CSV¤òJavaBeans?¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤Ë¤Ï¡¢CSV¤ÎÎó¤ÈJavaBeans?¤Î¥Õ¥£¡¼¥ë¥É¤òÂбþ¤Å¤±¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¤¬¡¢
¤Ê¤É¤¤¤í¤¤¤í¤ÊÊýË¡¤ÇÂбþÉÕ¤±¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£ JavaBeans?¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë †¼ÂºÝ¤Ë¤ä¤Ã¤Æ¤ß¤Þ¤¹¡£Mapping¤Î»ØÄê¤Ï¡¢
public class HeaderColumnNameTranslateMain { private static final String CSV_FILE = "sample.csv"; public static void main(String[] args) throws IOException { HeaderColumnNameTranslateMappingStrategy strat = new HeaderColumnNameTranslateMappingStrategy(); strat.setType(CSVSampleBean.class); Map<String, String> map = createMapping(); strat.setColumnMapping(map); CsvToBean csv = new CsvToBean(); List<CSVSampleBean> list = csv.parse(strat, new FileReader(CSV_FILE)); for (CSVSampleBean bean : list) { System.out.println(bean); } } // CSV¤Î¥Ø¥Ã¥À̾¤¬¡¢¤É¤Î¥Õ¥£¡¼¥ë¥É¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ì¤Ð¤¤¤¤¤«¤ò»ØÄꤹ¤ë¡£ // map.put("[¥Ø¥Ã¥À̾]", "[¥Õ¥£¡¼¥ë¥É̾]"); // ¤³¤Î¾ì¹ç¤Ï¡¢¥Ø¥Ã¥À¹Ô¤¬Ìµ»ë¤µ¤ì¤ë¡£ private static Map<String, String> createMapping() { Map<String, String> map = new HashMap<String, String>(); map.put("À«", "last_name"); map.put("̾", "first_name"); map.put("ǯÎð", "age"); return map; } } ¼Â¹Ô·ë²Ì¤Ï°Ê²¼¤ÎÄ̤ê nu.mine.kino.csv.CSVSampleBean@833955[À«=¤È¤¦¤¤ç¤¦,̾=¤¿¤í¤¦,ǯÎð=33] nu.mine.kino.csv.CSVSampleBean@13582d[À«=Åìµþ,̾=ÂÀϺ,ǯÎð=15] ¤³¤Î¤è¤¦¤Ë¥Þ¥Ã¥Ô¥ó¥°¤ò»ØÄꤹ¤ë¤À¤±¤Ç¡¢¼«Æ°Åª¤ËJavaBeans?¤òÀ¸À®¤·¥Õ¥£¡¼¥ë¥É¤ËÃͤò¥»¥Ã¥È¤·¤Æ¤¯¤ì¤Þ¤¹¡£¤³¤ì¤ÏÊØÍø¤Ç¤¹¤Í¡£ ¤Á¤Ê¤ß¤Ë List<CSVSampleBean> list = csv.parse(strat, new FileReader(CSV_FILE)); ¤³¤Î½èÍý¤Ç¤¹¤¬¡¢ CSVReader reader = new CSVReader(new FileReader(CSV_FILE)); List<CSVSampleBean> list = csv.parse(strat, reader); ¤È¤¤¤¦¤è¤¦¤Ë¼«Ê¬¤ÇCSVReader¤òÀ¸À®¤·¤ÆÅϤ¹¤³¤È¤â¤Ç¤¤Þ¤¹¡£¤³¤Á¤é¤Î¥á¥½¥Ã¥É¤ÏCSV¥Õ¥¡¥¤¥ë¤¬¥¿¥Ö¶èÀÚ¤ê¤À¤Ã¤¿¤Ð¤¢¤¤¤Ê¤É¤Ë CSVReader reader = new CSVReader(new FileReader(CSV_FILE),'\t'); <-¥»¥Ñ¥ì¡¼¥¿¤ò¥¿¥Ö¤Ë¤·¤Æ¤ë ¤È¤·¤Æ¼«Ê¬¤ÇCSVReader¤òºîÀ®¤¹¤ë¤Ð¤¢¤¤¤Ë»ÈÍѤ·¤Þ¤¹¡£ ¥×¥í¥°¥é¥àÃæ¤Ç¤Ê¤¯¡¢ÀßÄê¥Õ¥¡¥¤¥ë¤È¤«¤Ç¥Þ¥Ã¥Ô¥ó¥°¤ò»ØÄꤷ¤¿¤¤ †¤µ¤Æ¥×¥í¥°¥é¥àÃæ¤ËMap¤òÍѤ¤¤ÆCSV¥Ø¥Ã¥À̾¤È¥Õ¥£¡¼¥ë¥É̾¤òÂбþ¤Å¤±¤ë¤³¤È¤Ç¡¢JavaBeans?¤ËCSV¤ò¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤·¤¿¡£¤Ç¤Ï¤½¤Î¥Þ¥Ã¥Ô¥ó¥°¤ò³°¤«¤é»ØÄê¤Ç¤¤Ê¤¤¤«¤Ç¤¹¤¬¡¢¤É¤¦¤âɸ½à¤Îµ¡Ç½¤Ç¤Ï¤Ç¤¤Ê¤¤¤Ã¤Ý¤¤¤Ç¤¹¡£¡¢¡¢¡¢¤Î¤Ç¤Á¤ç¤Ã¤È¥³¡¼¥É¤òÄɲ䷤ơ¢¤Ç¤¤ë¤è¤¦¤Ë¤·¤Þ¤·¤¿¡£ au.com.bytecode.opencsv.bean.HeaderColumnNameAutoTranslateMappingStrategy¤È¤¤¤¦¥¯¥é¥¹¤òºîÀ®¤·¤¿¤Î¤Ç¤¹¤¬¡¢¤³¤Î¥¯¥é¥¹¤Ï À«=last_name ̾=first_name ǯÎð=age ¤È¤¤¤¦¥Æ¥¥¹¥È¥Õ¥¡¥¤¥ë*1¤«¤é¥Þ¥Ã¥Ô¥ó¥°¤òÆɤ߼è¤ê¡¢¤½¤Î¥Þ¥Ã¥Ô¥ó¥°¤Ë¤·¤¿¤¬¤Ã¤ÆJavaBeans?¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤·¤Æ¤¯¤ì¤Þ¤¹¡£ ¶ñÂÎŪ¤Ê¥³¡¼¥É¤Ï°Ê²¼¤ÎÄ̤ꡣ public class HeaderColumnNameTranslateMain3 { private static final String CSV_FILE = "sample.csv"; public static void main(String[] args) throws IOException { HeaderColumnNameAutoTranslateMappingStrategy strat = new HeaderColumnNameAutoTranslateMappingStrategy(); // FileInputStream in = new FileInputStream(new File("hogehoge.txt")); // strat.setInputStream(in); strat.setType(CSVSampleBean.class); CsvToBean csv = new CsvToBean(); List<CSVSampleBean> list = csv.parse(strat, new FileReader(CSV_FILE)); for (CSVSampleBean bean : list) { System.out.println(bean); } } } ¥³¥á¥ó¥È¥¢¥¦¥È¤·¤Æ¤Þ¤¹¤¬¡¢ÀßÄê¥Õ¥¡¥¤¥ë¤Ï¥×¥í¥°¥é¥à¤Ç»ØÄꤹ¤ë¤³¤È¤â¤Ç¤¤Þ¤¹¡£»ØÄꤷ¤Ê¤¤¾ì¹ç¤Î¥Ç¥Õ¥©¥ë¥ÈÃͤϡ¢JavaBeans?¤Î¥¯¥é¥¹¤ÈƱ¤¸¥Ç¥£¥ì¥¯¥È¥ê¾å¤Î[JavaBean?¥¯¥é¥¹Ì¾].txt¤Ç¤¹¡£ TIPS½¸ †°Ê²¼½ñ¤ÅÓÃæ¡£ CSV¤Î½çÈÖ¤ÈJavaBeans?¤Î¥Õ¥£¡¼¥ë¥É¤ò¥Þ¥Ã¥Ô¥ó¥°¤¹¤ëÊýË¡ †CSV¤Î½çÈÖ(²¿ÎóÌܤΥǡ¼¥¿)¤ÈJavaBeans?¤Î¥Õ¥£¡¼¥ë¥É¤ò¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤³¤È¤Ç¡¢JavaBeans?¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÀ¸À®¤¹¤ë»ö¤â¤Ç¤¤Þ¤¹¡£¶ñÂÎŪ¤Ë¤Ï²¼µ¤Î¤è¤¦¤Ë¡¢ ColumnPositionMappingStrategy?¤ò¤Ä¤«¤¨¤Ð£Ï£Ë¤Ç¤¹¡£ ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy(); strat.setType(CSVSampleBean.class); // CSV¤Î½çÈ֤ǡ¢¤É¤Î¥Õ¥£¡¼¥ë¥É¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ì¤Ð¤¤¤¤¤«¤ò»ØÄꤹ¤ë¡£ String[] columns = new String[] { "last_name", "first_name", "age" };//¥Õ¥£¡¼¥ë¥É̾ strat.setColumnMapping(columns); ¤É¤Î¥Õ¥£¡¼¥ë¥É¤ËCSV¤Î¤É¤ÎÎó¤Î¥Ç¡¼¥¿¤ò¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤«¤ò»ØÄꤹ¤ë¤Ã¤Æ´¶¤¸¤Ç¤¹¤Í¡£ CSV¤Î¥Ø¥Ã¥À¹Ô¤Î¥Ø¥Ã¥À̾¾Î¤È¥Õ¥£¡¼¥ë¥É¤ò¥Þ¥Ã¥Ô¥ó¥°¤¹¤ëÊýË¡ †HeaderColumnNameTranslateMappingStrategy?¤ò¤Ä¤«¤¦ CSV¤Î¥Ø¥Ã¥À¹Ô¤Î¥Ø¥Ã¥À̾¾Î¤¬¤½¤Î¤Þ¤Þ¥Õ¥£¡¼¥ë¥É̾¤Î¾ì¹ç †HeaderColumnNameMappingStrategy?¤ò¤Ä¤«¤¦ ´ØÏ¢¥ê¥ó¥¯ †¤³¤Îµ»ö¤Ï ¸½ºß¤Î¥¢¥¯¥»¥¹:43627 |