¤³¤Î¥Ú¡¼¥¸¤òdel.icio.us¤ËÄɲà ¤³¤Î¥Ú¡¼¥¸¤ò¤Ï¤Æ¤Ê¥Ö¥Ã¥¯¥Þ¡¼¥¯¤ËÄɲ䳤Υڡ¼¥¸¤ò´Þ¤à¤Ï¤Æ¤Ê¥Ö¥Ã¥¯¥Þ¡¼¥¯ ¤³¤Î¥Ú¡¼¥¸¤òlivedoor ¥¯¥ê¥Ã¥×¤ËÄɲà ¤³¤Î¥Ú¡¼¥¸¤òYahoo!¥Ö¥Ã¥¯¥Þ¡¼¥¯¤ËÄɲ䳤Υڡ¼¥¸¤ò´Þ¤àYahoo!¥Ö¥Ã¥¯¥Þ¡¼¥¯
Top / Java / CSV¤ò¼è¤ê°·¤¦

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¤è¤ê¥À¥¦¥ó¥í¡¼¥É²Äǽ¤Ç¤¹¡£

¥µ¥ó¥×¥ë¤Ï°Ê²¼¤Î¤è¤¦¤Ê´¶¤¸¤Ç¤¹

  • sample.csv
    À«,̾,ǯÎð
    ¤È¤¦¤­¤ç¤¦,¤¿¤í¤¦,33
    Åìµþ,ÂÀϺ,15
  • ¥×¥í¥°¥é¥à
    public class Main {
      public static void main(String[] args) throws IOException {
        CSVReader reader = new CSVReader(new FileReader("sample.csv"));
        String[] nextLine;
        while ((nextLine = reader.readNext()) != null) {
          // nextLine[] is an array of values from the line
          System.out.println(nextLine[0] + " " + nextLine[1] + " etc...");
        }
      }
    }

¼Â¹Ô·ë²Ì¤Ï°Ê²¼¤ÎÄ̤ꡣ

À« ̾ etc...
¤È¤¦¤­¤ç¤¦ ¤¿¤í¤¦ etc...
Åìµþ ÂÀϺ etc...

csv¥Õ¥¡¥¤¥ë¤ò¥Ñ¡¼¥¹¤·¤Æ¡¢Å¬ÀÚ¤Ëʬ³ä¤·¤Æ¤¯¤ì¤Æ¤¤¤Þ¤¹¡£

¥µ¥ó¥×¥ë¤Ïñ½ã¤Ç¤·¤¿¤¬¡¢¥À¥Ö¥ë¥¯¥©¡¼¥È("")¤Ç°Ï¤Þ¤ì¤¿CSV¤ä¥¿¥Ö¶èÀÚ¤ê¤Î¥Õ¥¡¥¤¥ë¡¢""Æâ¤Î¥«¥ó¥Þ¤Î¼è¤ê°·¤¤¡¢ÅÓÃæ¤Ë²þ¹Ô¤¬Æþ¤ëCSV¡¢¤¿¤È¤¨¤Ð

À«,̾,ǯÎð
¤È¤¦¤­¤ç¤¦,"¤¿
¤í¤¦",33
Åìµþ,ÂÀϺ,15

¤³¤ó¤Ê¤¿¤Á°­¤¤CSV¤Ê¤É¤â¡¢Àµ¤·¤¯¼è¤ê°·¤¦¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£¤¹¤´¤¤¤Ç¤¹¡£¡£

JavaBeans?¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë

¤µ¤ÆÀè¤Î¤è¤¦¤Ë¡¢´Êñ¤ËCSV¤ò¥Ñ¡¼¥¹¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤·¤¿¤¬¡¢¤³¤Î¥é¥¤¥Ö¥é¥ê¤Ç¤È¤Æ¤âÊØÍø¤Ê¤Î¤Ï¡¢CSV¥Ç¡¼¥¿¤ò»ØÄꤷ¤¿JavaBeans?¤ËľÀܥޥåԥ󥰤Ǥ­¤ë¤È¤³¤í¤Ç¤¹¡£¤¿¤È¤¨¤Ð¤µ¤Ã¤­¤Î¡¢

  • sample.csv
    À«,̾,ǯÎð
    ¤È¤¦¤­¤ç¤¦,¤¿¤í¤¦,33
    Åìµþ,ÂÀϺ,15
    ¤ò¡¢
  • JavaBeans?
    public class CSVSampleBean implements Serializable {
      private String last_name;
      private String first_name;
      private String age;
      // Getter/Setter¤Ï¾Êά
      @Override
      public String toString() {
        return new ToStringBuilder(this).append("À«", last_name).append("̾",
            first_name).append("ǯÎð", age).toString();
      }
    }

¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë»ö¤¬¤Ç¤­¤ë¤ó¤Ç¤¹¤Í¡£CSV¤òJavaBeans?¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë¤Ë¤Ï¡¢CSV¤ÎÎó¤ÈJavaBeans?¤Î¥Õ¥£¡¼¥ë¥É¤òÂбþ¤Å¤±¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¤¬¡¢

  • CSV¤Î£±ÎóÌܤϥե£¡¼¥ë¥É̾¡û¡û¡¢£²ÎóÌܤϢ¤¢¤ ¤È»ØÄꤹ¤ë
  • CSV¤Î£±¹ÔÌܤϥإåÀ¹Ô¤È¸«¤Ê¤·¤Æ¡¢£±¹ÔÌܤÎʸ»úÎ󷲤ȡ¢JavaBeans?¤Î¥Õ¥£¡¼¥ë¥É¤òÂбþ¤Å¤±¤ë

¤Ê¤É¤¤¤í¤¤¤í¤ÊÊýË¡¤ÇÂбþÉÕ¤±¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

JavaBeans?¤Ë¥Þ¥Ã¥Ô¥ó¥°¤¹¤ë

¼ÂºÝ¤Ë¤ä¤Ã¤Æ¤ß¤Þ¤¹¡£Mapping¤Î»ØÄê¤Ï¡¢

  • sample.csv
    À«,̾,ǯÎð
    ¤È¤¦¤­¤ç¤¦,¤¿¤í¤¦,33
    Åìµþ,ÂÀϺ,15
    ...
    ¤Î¡ÖÀ«,̾,ǯÎð¡×¤ÈJavaBeans?¤Î¥Õ¥£¡¼¥ë¥É¡Ölast_name,first_name,age¡×¤òjava.util.Map¤ò¤Ä¤«¤Ã¤ÆÂбþ¤Å¤±¤ëÊýË¡¤Ç¤ä¤Ã¤Æ¤ß¤Þ¤¹¡£¼ÂºÝ¤Î¥×¥í¥°¥é¥à¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¹
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?¤ò¤Ä¤«¤¦

´ØÏ¢¥ê¥ó¥¯


¤³¤Îµ­»ö¤Ï

ÁªÂò»è Åêɼ
¤ª¤â¤·¤í¤«¤Ã¤¿ 47  
¤½¤¦¤Ç¤â¤Ê¤¤ 3  

¤³¤Î¥Ú¡¼¥¸¤òdel.icio.us¤ËÄɲà ¤³¤Î¥Ú¡¼¥¸¤ò¤Ï¤Æ¤Ê¥Ö¥Ã¥¯¥Þ¡¼¥¯¤ËÄɲ䳤Υڡ¼¥¸¤ò´Þ¤à¤Ï¤Æ¤Ê¥Ö¥Ã¥¯¥Þ¡¼¥¯ ¤³¤Î¥Ú¡¼¥¸¤òlivedoor ¥¯¥ê¥Ã¥×¤ËÄɲà ¤³¤Î¥Ú¡¼¥¸¤òYahoo!¥Ö¥Ã¥¯¥Þ¡¼¥¯¤ËÄɲ䳤Υڡ¼¥¸¤ò´Þ¤àYahoo!¥Ö¥Ã¥¯¥Þ¡¼¥¯
Top / Java / CSV¤ò¼è¤ê°·¤¦

¸½ºß¤Î¥¢¥¯¥»¥¹:43627


*1 ³ºÅö¤ÎJavaBeans?¤ÈƱ¤¸¥Ç¥£¥ì¥¯¥È¥ê¤Ë[Bean̾].txt¤Ã¤Æ̾Á°¤ÇÃÖ¤¤¤Æ¤ª¤¯

¥È¥Ã¥×   ÊÔ½¸ Åà·ë º¹Ê¬ ¥Ð¥Ã¥¯¥¢¥Ã¥× źÉÕ Ê£À½ ̾Á°Êѹ¹ ¥ê¥í¡¼¥É   ¿·µ¬ °ìÍ÷ ñ¸ì¸¡º÷ ºÇ½ª¹¹¿·   ¥Ø¥ë¥×   ºÇ½ª¹¹¿·¤ÎRSS
Last-modified: 2013-05-22 (¿å) 09:51:25 (181d)