1 package jp.co.areaweb.tools.csv;
\r
3 import java.util.LinkedList;
\r
5 import jp.co.areaweb.tools.csv.CsvRecord;
\r
8 * <p>CSVファイルの読み取りを行う。<br>
\r
9 * このクラスではCSVに対するファイル操作と、各行を束ねた単位で操作します。各行は、CsvRecordオブジェクトで管理します。<br>
\r
10 * このクラスは通常,CsvRecordクラスとセットで利用します。</p>
\r
12 * (読み出し可能ファイルサイズ=最大8MBまで)
\r
15 * @version v2.23 2010/07/27
\r
16 * @see jp.co.areaweb.tools.csv.CsvRecord
\r
19 public class CsvReader
\r
23 * (読み出し可能ファイルサイズ=最大8MBまで)
\r
26 * @throws Exception error
\r
28 public static void main(String[] args) throws Exception {
\r
29 File file = new File("test.csv");
\r
30 CsvReader obj = new CsvReader(file, true, ",");
\r
31 int size = obj.load();
\r
32 System.out.println("line count = "+ obj.size());
\r
34 for (int i=0; i < size; i++) {
\r
35 System.out.print(String.format("%05d : ", i));
\r
36 CsvRecord rec = obj.get(i);
\r
38 System.out.println(rec.toString());
\r
41 System.out.println("< null >");
\r
49 boolean titleLine = false;
\r
52 protected String charsetName = "Windows-31J";
\r
53 LineNumberReader reader;
\r
57 * fileで指定されたファイルを対象のCSVファイルとします.
\r
59 * @param file 対象のCSVファイル
\r
60 * @throws IOException 例外
\r
61 * @throws UnsupportedEncodingException 例外
\r
63 public CsvReader(File file) throws UnsupportedEncodingException, IOException {
\r
64 this(file, false, ",");
\r
69 * fileで指定されたファイルを対象のCSVファイルとします.
\r
71 * @param file 対象のCSVファイル
\r
72 * @param titleLine boolean
\r
73 * @param split string
\r
74 * @throws IOException 例外
\r
75 * @throws UnsupportedEncodingException 例外
\r
77 public CsvReader(File file, boolean titleLine, String split) throws UnsupportedEncodingException, IOException {
\r
79 this.titleLine = titleLine;
\r
85 * CVSファイルからデータを読みだします。
\r
86 * (読み出し可能ファイルサイズ=最大8MBまで)
\r
87 * @throws Exception 例外
\r
90 public int load() throws Exception {
\r
91 this.reader = new LineNumberReader(new InputStreamReader(new FileInputStream(this.file), this.charsetName));
\r
92 this.reader.mark(1024 * 1024 * 8);
\r
94 this.columnSize = 0;
\r
95 while (reader.readLine() != null) {
\r
102 public CsvRecord get(int index) {
\r
103 if ((index < 0) || (index > this.size)) {
\r
107 this.reader.reset();
\r
109 for (int i=0; i < index; i++) {
\r
113 String line = reader.readLine();
\r
114 if (line != null) {
\r
115 CsvRecord record = new CsvRecord();
\r
116 record.analizeRecord(line, this.split);
\r
120 catch (IOException e) {
\r
121 e.printStackTrace();
\r
127 * index番目の項目がdataStrになっているレコードを読み出す。
\r
128 * @param index 検索対象とする列番号(0~)
\r
129 * @param dataStr 検索値
\r
130 * @return 条件に合致したレコード(複数)
\r
131 * @throws IOException 例外
\r
133 public LinkedList<CsvRecord> find(int index, String dataStr) throws IOException {
\r
134 if ((index < 0) || (index > this.size)) {
\r
137 LinkedList<CsvRecord> result = new LinkedList<CsvRecord>();
\r
138 this.reader.reset();
\r
140 for (int i=0; i < this.size; i++) {
\r
141 String line = reader.readLine();
\r
142 if (line != null) {
\r
143 CsvRecord record = new CsvRecord();
\r
144 record.analizeRecord(line, this.split);
\r
145 String item = record.get(index);
\r
146 if ((item != null) && item.trim().equals(dataStr)) {
\r
147 result.add(record);
\r
154 public int size() {
\r
159 * インポート元のキャラクタセットを設定する。
\r
160 * デフォルトは、「Windows31J」
\r
161 * @param charsetName 対象のCSVファイル内の文字コード
\r
163 public void setCharsetName(String charsetName) {
\r
164 this.charsetName = charsetName;
\r
168 * 現在設定されているCSVファイルの文字コードを取得する
\r
169 * @return 現在設定されている文字コードを取得する
\r
171 public String getCharsetName() {
\r
172 return this.charsetName;
\r