SuperCSVのサンプル
CSVライブラリの決定版? - SuperCSV 1.0登場
郵便番号CSVの読み込みサンプルを作ってみる。
CSVの一列をBeanにマッピングするメゾットを使用して一行単位で処理する。
郵便番号CSV
SuperCSV-1.16
commons-lang-2.3←Beanの中身を出力するため。
BeanAddress.java
import org.apache.commons.lang.builder.ToStringBuilder; /** * <HR> * <P> * ■モジュール名 <BR> * <BLOCKQUOTE> * BeanAddress.java <BR> * </BLOCKQUOTE> * <P> * ■クラス仕様 <BR> * <BLOCKQUOTE> * BeanAddressクラスは住所を保持するクラスです。<BR> * </BLOCKQUOTE> * <P> * <HR> * <P> */ public class BeanAddress { /** * 全国地方公共団体コード JIS X0401,X0402 */ private String la_code; /** * 旧郵便番号 5桁 */ private String zip_code_old; /** * 郵便番号 7桁 */ private String zip_code; /** * 都道府県名仮名 */ private String prefectural_kana; /** * 市区町村名仮名 */ private String city_kana; /** * 町域名仮名 */ private String town_kana; /** * 都道府県名 */ private String prefectural; /** * 市区町村名 */ private String city; /** * 町域名 */ private String town; /** * 一町域が二以上の郵便番号で表される場合の表示 1:該当 0:該当せず */ private String plurally; /** * 小字毎に番地が起番されている町域の表示 1:該当 0:該当せず */ private String small_block; /** * 丁目を有する町域の場合の表示 1:該当 0:該当せず */ private String street; /** * 一つの郵便番号で二以上の町域を表す場合の表示 1:該当 0:該当せず */ private String share; /** * 更新 0:変更なし 1:変更あり 2:廃止(廃止データのみ使用) */ private String update; /** * 変更理由 0:変更なし 1:市政・区政・町政・分区・政令指定都市施行 * 2:住居表示の実施 3:区画整理 4:郵便区調整等 5:訂正 * 6:廃止(廃止データのみ使用) */ private String reason; /** * * 説明文。 * @return */ public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCity_kana() { return city_kana; } public void setCity_kana(String city_kana) { this.city_kana = city_kana; } public String getLa_code() { return la_code; } public void setLa_code(String la_code) { this.la_code = la_code; } public String getPlurally() { return plurally; } public void setPlurally(String plurally) { this.plurally = plurally; } public String getPrefectural() { return prefectural; } public void setPrefectural(String prefectural) { this.prefectural = prefectural; } public String getPrefectural_kana() { return prefectural_kana; } public void setPrefectural_kana(String prefectural_kana) { this.prefectural_kana = prefectural_kana; } public String getReason() { return reason; } public void setReason(String reason) { this.reason = reason; } public String getShare() { return share; } public void setShare(String share) { this.share = share; } public String getSmall_block() { return small_block; } public void setSmall_block(String small_block) { this.small_block = small_block; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getTown() { return town; } public void setTown(String town) { this.town = town; } public String getTown_kana() { return town_kana; } public void setTown_kana(String town_kana) { this.town_kana = town_kana; } public String getUpdate() { return update; } public void setUpdate(String update) { this.update = update; } public String getZip_code() { return zip_code; } public void setZip_code(String zip_code) { this.zip_code = zip_code; } public String getZip_code_old() { return zip_code_old; } public void setZip_code_old(String zip_code_old) { this.zip_code_old = zip_code_old; } /** * * 説明文\。 * @return */ public String toString() { return ToStringBuilder.reflectionToString(this); } }
InputCsv.java
import java.io.FileReader; import java.io.IOException; import org.supercsv.io.CsvBeanReader; import org.supercsv.prefs.CsvPreference; /** * <HR> * <P> * ■モジュール名 <BR> * <BLOCKQUOTE> * InputCsv.java <BR> * </BLOCKQUOTE> * <P> * ■クラス仕様 <BR> * <BLOCKQUOTE> * InputCsvクラスはCSVの読み込みクラスです。<BR> * </BLOCKQUOTE> * <P> * <HR> * <P> */ public class InputCsv extends CsvBeanReader { /** * 住所 */ private BeanAddress address; /** * ネームマッピング */ private static final String[] NAME_MAPPING = {"la_code" , "zip_code_old", "zip_code", "prefectural_kana", "city_kana" , "town_kana", "prefectural", "city", "town", "plurally", "small_block" , "street", "share", "update", "reason"}; /** * * コンストラクタ。 * @param path 入力ファイル * @throws Exception 例外 */ public InputCsv(final String path) throws Exception { super(new FileReader(path), CsvPreference.NO_COMMENT_PREFERENCE); } /** * * 読み込み。 * @return * @throws Exception */ public boolean readNext() throws Exception{ this.address = super.read(BeanAddress.class, NAME_MAPPING); if (this.address == null) { return false; } else { return true; } } /** * * Addressを取得する。 * @return */ public BeanAddress getAddress() { return address; } /** * * 閉じる */ public void close() throws RuntimeException { try { super.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
SuperCsvTest.java
/** * <HR> * <P> * ■モジュール名 <BR> * <BLOCKQUOTE> * SuperCsvTest.java <BR> * </BLOCKQUOTE> * <P> * ■クラス仕様 <BR> * <BLOCKQUOTE> * SuperCsvTestクラスはテストクラスです。<BR> * </BLOCKQUOTE> * <P> * <HR> * <P> */ public class SuperCsvTest { /** * * メインメゾット * @param args */ public static void main(String[] args) { InputCsv ic = null; try { ic = new InputCsv("./13TOKYO.CSV"); int count = 0; while (ic.readNext()) { BeanAddress address = ic.getAddress(); System.out.println(address.toString()); count++; } System.out.println(count); } catch (Exception e) { e.printStackTrace(); } finally { if (ic != null) { ic.close(); } } } }
もうちょっとシンプルに出来そう。