一般的なプロジェクトの設定ファイルとか、メッセージファイルとか、普通はXMLファイルに保存としますが、マイクロソフトのOffice Ecxelが広くて使用されてますので、CSVを操作するところも多いです。

CSVでは、データの各要素をカンマ(「,」)で区切り、改行がそのままデータ行の区切りを表します。

CSVデータは、一般に「.csv」という拡張子の付いたテキストファイルとして保存されます。テキストエディタだけでなく、Microsoft Excelなどの表計算ソフトでも読み込むことができ、閲覧や編集が簡単なため、よく利用されます。

以下のクラスには、BufferedReaderとFileReader利用して、CSV形式のファイルを読みかけます。

Download: CsvUtil.java
  1. package com.csv.util;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9.  
  10. public class CsvUtil {
  11.  private String filename = null;
  12.  private BufferedReader bufferedreader = null;
  13.  private List list = new ArrayList();
  14.  
  15.  public CsvUtil() {
  16.  }
  17.  
  18.  /**
  19.   *
  20.   * @param filename    CSVファイル名
  21.   * @throws IOException
  22.   */
  23.  public CsvUtil(String filename) throws IOException {
  24.   this.filename = filename;
  25.   bufferedreader = new BufferedReader(new FileReader(filename));
  26.   String stemp;
  27.   while ((stemp = bufferedreader.readLine()) != null) {
  28.    list.add(stemp);
  29.   }
  30.  }
  31.  
  32.  /**
  33.   * CSV内容格納リストを取得します。
  34.   * @return CSV内容リスト
  35.   * @throws IOException
  36.   */
  37.  public List getList() throws IOException {
  38.   return list;
  39.  }
  40.  
  41.  /**
  42.   * CSV行数を取得します。
  43.   * @return CSV行数
  44.   */
  45.  public int getRowNum() {
  46.   return list.size();
  47.  }
  48.  
  49.  /**
  50.   * 列数を取得します。
  51.   * @return CSV列数
  52.   */
  53.  public int getColNum() {
  54.   if (!list.toString().equals("[]")) {
  55.    if (list.get(0).toString().contains(",")) {
  56.     return list.get(0).toString().split(",").length;
  57.    } else if (list.get(0).toString().trim().length() != 0) {
  58.     return 1;
  59.    } else {
  60.     return 0;
  61.    }
  62.   } else {
  63.    return 0;
  64.   }
  65.  }
  66.  
  67.  
  68.  /**
  69.   * 指定されたインデックス内容を取得します(行)。
  70.   */
  71.  public String getRow(int index) {
  72.   if (this.list.size() != 0)
  73.    return (String) list.get(index).toString().trim();
  74.   else
  75.    return null;
  76.  }
  77.  
  78.  /**
  79.   * 指定されたインデックス内容を取得します(列)。
  80.   * @param index
  81.   * @return
  82.   */
  83.  public String getCol(int index) {
  84.   if (this.getColNum() == 0) {
  85.    return null;
  86.   }
  87.   StringBuffer scol = new StringBuffer();
  88.   String temp = null;
  89.   int colnum = this.getColNum();
  90.   if (colnum > 1) {
  91.    for (Iterator it = list.iterator(); it.hasNext();) {
  92.     temp = it.next().toString().trim();
  93.    
  94.     scol = scol.append(temp.split(",")[index] + ",");
  95.    }
  96.   } else {
  97.    for (Iterator it = list.iterator(); it.hasNext();) {
  98.     temp = it.next().toString().trim();
  99.     scol = scol.append(temp + ",");
  100.    }
  101.   }
  102.   String str=new String(scol.toString());
  103.   str = str.substring(0, str.length() - 1);
  104.   return str;
  105.  }
  106.  
  107.  /**
  108.   * 指定された行、列の内容を取得します。
  109.   * @param row
  110.   * @param col
  111.   * @return
  112.   */
  113.  public String getString(int row, int col) {
  114.   String temp = null;
  115.   int colnum = this.getColNum();
  116.   if (colnum > 1) {
  117.    temp = list.get(row).toString().split(",")[col].trim();
  118.   } else if (colnum == 1) {
  119.    temp = list.get(row).toString().trim();
  120.   } else {
  121.    temp = null;
  122.   }
  123.   return temp;
  124.  }
  125.  
  126.  /**
  127.   * 開いてCSVファイルをクローズします。
  128.   * @throws IOException
  129.   */
  130.  public void CsvClose() throws IOException {
  131.   this.bufferedreader.close();
  132.  }
  133.  
  134.  /**
  135.   * テストソースコード
  136.   * @throws IOException
  137.   */
  138.  public void test() throws IOException {
  139.   CsvUtil cu = new CsvUtil("G:/Book1.csv");
  140.   List tt = cu.getList();
  141.   for (Iterator itt = tt.iterator(); itt.hasNext();) {
  142.    System.out.println(itt.next().toString());
  143.   }
  144.   System.out.println(cu.getRowNum());
  145.   System.out.println(cu.getColNum());
  146.   System.out.println(cu.getRow(0));
  147.   System.out.println(cu.getCol(0));
  148.   System.out.println(cu.getString(0, 0));
  149.   cu.CsvClose();
  150.  
  151.  }
  152.  
  153.  /**
  154.   * メイン
  155.   * @param args
  156.   * @throws IOException
  157.   */
  158.  public static void main(String[] args) throws IOException {
  159.   CsvUtil test = new CsvUtil();
  160.   test.test();
  161.  }
  162. }
メインコンテンツEND ■
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.67 out of 5)
Loading ... Loading ...

Posted on Monday, 1st December 2008 by admin

Tags: , , , , ,
Posted in Java | Comments (2) | 20,101 views

2 Responses to “JavaでCSV形式のファイルを読み書きするライブラリソースと使用例(BufferedReaderとFileReaderを利用する)”

  1. Javaでiniファイルを読み書きクラスソース | DigiTechLog Dot Com Says:

    [...] この前の「JavaでCSV形式のファイルを読み書きするライブラリソースと使用例(BufferedReaderとFileReaderを利用する)」には、JavaでCSVファイルの読み書きすることを紹介しました。今回は、Ja [...]

  2. Javaでsplitを使ってCSVファイル読み込み | DigiTechLog Dot Com Says:

    [...] その前の「JavaでCSV形式のファイルを読み書きするライブラリソースと使用例(BufferedReaderとFileReaderを利用する)」の中にBufferedReaderとFileReader利用して、CSV形式のファイル読み込む処理を [...]

Leave a Reply