CSVファイルを読み込む。
表データの読み込みに、何かと便利なCSVファイル。
こいつを二次元の文字列配列に読み込む。
■ソースコード
まあ、こんな感じ。
package test; import java.io.BufferedReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CSVTest { private static final String REG="\"([^\"\\\\]*(\\\\.[^\"\\\\]*)*)\"|([^,]+)|,|"; public static String[][] parse(BufferedReader reader,boolean existTitle) throws IOException{ ArrayList<String[]> list=new ArrayList<String[]>(); Pattern pattern = Pattern.compile(REG); String line; if(existTitle)line=reader.readLine(); while((line=reader.readLine())!=null){ String[] sp=split(pattern,line); list.add(sp); } if(list.size()==0)return new String[0][0]; return list.toArray(new String[list.size()][]); } private static String[] split(Pattern pattern,String line){ Matcher matcher=pattern.matcher(line); List<String> list=new ArrayList<String>(); int index=0; int com=0; while(index<line.length()){ if(matcher.find(index+com)){ String s=matcher.group(); index=matcher.end(); list.add(s); com=1; } } return list.toArray(new String[list.size()]); } }