一般的なプロジェクトの設定ファイルとか、メッセージファイルとか、普通はXMLファイルに保存としますが、マイクロソフトのOffice Ecxelが広くて使用されてますので、CSVを操作するところも多いです。
CSVでは、データの各要素をカンマ(「,」)で区切り、改行がそのままデータ行の区切りを表します。
CSVデータは、一般に「.csv」という拡張子の付いたテキストファイルとして保存されます。テキストエディタだけでなく、Microsoft Excelなどの表計算ソフトでも読み込むことができ、閲覧や編集が簡単なため、よく利用されます。
以下のクラスには、BufferedReaderとFileReader利用して、CSV形式のファイルを読みかけます。
Download: CsvUtil.java
- package com.csv.util;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class CsvUtil {
- private String filename = null;
- private BufferedReader bufferedreader = null;
- private List list = new ArrayList();
- public CsvUtil() {
- }
- /**
- *
- * @param filename CSVファイル名
- * @throws IOException
- */
- public CsvUtil(String filename) throws IOException {
- this.filename = filename;
- bufferedreader = new BufferedReader(new FileReader(filename));
- String stemp;
- while ((stemp = bufferedreader.readLine()) != null) {
- list.add(stemp);
- }
- }
- /**
- * CSV内容格納リストを取得します。
- * @return CSV内容リスト
- * @throws IOException
- */
- public List getList() throws IOException {
- return list;
- }
- /**
- * CSV行数を取得します。
- * @return CSV行数
- */
- public int getRowNum() {
- return list.size();
- }
- /**
- * 列数を取得します。
- * @return CSV列数
- */
- public int getColNum() {
- if (!list.toString().equals("[]")) {
- if (list.get(0).toString().contains(",")) {
- return list.get(0).toString().split(",").length;
- } else if (list.get(0).toString().trim().length() != 0) {
- return 1;
- } else {
- return 0;
- }
- } else {
- return 0;
- }
- }
- /**
- * 指定されたインデックス内容を取得します(行)。
- */
- public String getRow(int index) {
- if (this.list.size() != 0)
- return (String) list.get(index).toString().trim();
- else
- return null;
- }
- /**
- * 指定されたインデックス内容を取得します(列)。
- * @param index
- * @return
- */
- public String getCol(int index) {
- if (this.getColNum() == 0) {
- return null;
- }
- StringBuffer scol = new StringBuffer();
- String temp = null;
- int colnum = this.getColNum();
- if (colnum > 1) {
- for (Iterator it = list.iterator(); it.hasNext();) {
- temp = it.next().toString().trim();
- scol = scol.append(temp.split(",")[index] + ",");
- }
- } else {
- for (Iterator it = list.iterator(); it.hasNext();) {
- temp = it.next().toString().trim();
- scol = scol.append(temp + ",");
- }
- }
- String str=new String(scol.toString());
- str = str.substring(0, str.length() - 1);
- return str;
- }
- /**
- * 指定された行、列の内容を取得します。
- * @param row
- * @param col
- * @return
- */
- public String getString(int row, int col) {
- String temp = null;
- int colnum = this.getColNum();
- if (colnum > 1) {
- temp = list.get(row).toString().split(",")[col].trim();
- } else if (colnum == 1) {
- temp = list.get(row).toString().trim();
- } else {
- temp = null;
- }
- return temp;
- }
- /**
- * 開いてCSVファイルをクローズします。
- * @throws IOException
- */
- public void CsvClose() throws IOException {
- this.bufferedreader.close();
- }
- /**
- * テストソースコード
- * @throws IOException
- */
- public void test() throws IOException {
- CsvUtil cu = new CsvUtil("G:/Book1.csv");
- List tt = cu.getList();
- for (Iterator itt = tt.iterator(); itt.hasNext();) {
- System.out.println(itt.next().toString());
- }
- System.out.println(cu.getRowNum());
- System.out.println(cu.getColNum());
- System.out.println(cu.getRow(0));
- System.out.println(cu.getCol(0));
- System.out.println(cu.getString(0, 0));
- cu.CsvClose();
- }
- /**
- * メイン
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- CsvUtil test = new CsvUtil();
- test.test();
- }
- }
メインコンテンツEND ■
Posted on Monday, 1st December 2008 by admin
Tags: BufferedReader, CSV, Excel, FileReader, Java, XML
Posted in Java | Comments (2) | 20,101 views


(3 votes, average: 3.67 out of 5)
January 8th, 2009 at 12:15 am
[...] この前の「JavaでCSV形式のファイルを読み書きするライブラリソースと使用例(BufferedReaderとFileReaderを利用する)」には、JavaでCSVファイルの読み書きすることを紹介しました。今回は、Ja [...]
May 11th, 2009 at 2:30 pm
[...] その前の「JavaでCSV形式のファイルを読み書きするライブラリソースと使用例(BufferedReaderとFileReaderを利用する)」の中にBufferedReaderとFileReader利用して、CSV形式のファイル読み込む処理を [...]