小第不才,从google里搜索出了一个读CSV文件的好方法,我通过垃圾的日化,现程现给大家,希望能够帮助那些有困难的朋友.
/** * 方法はCSVファイルを読むことができ、ArrayListを返します。 * @param fullPathFilename CSVファイルのディレクトリー * @param numToRead 方法はどれだけのラインを(読み取り)もたらすだろうか * @return arrlist * @throws IOException */ public ArrayList readCSVTwoParam(String fullPathFilename,int numToRead) throws IOException{ BufferedReader reader = new BufferedReader(new FileReader(fullPathFilename)); String line; StringTokenizer st; int size = 0; int index = 0; int pos = 1; //第1個の位置を越える String[][] data; String[][] temp; final int DEFAULT_SIZE = 250; final String DELIMITER = ","; int maxRecords; ArrayList arrlist = null; ArrayList arrlist1 = null;
//読み取りラインの数を計算します。 line = reader.readLine(); st = new StringTokenizer(line, DELIMITER); while (st.hasMoreElements()) { st.nextElement(); size++; }
//配列を分類する、場合、セットされないパラメーター、全ファイルを読む、 //要求されるようにそうでなければ止まります。 if (numToRead == 0) { numToRead = DEFAULT_SIZE; maxRecords = Integer.MAX_VALUE; } else { maxRecords = numToRead; }
data = new String[numToRead][size];
//配列を増すためにそれを再び行います。 st = new StringTokenizer(line, DELIMITER); while (st.hasMoreElements()) { data[0][index] = st.nextElement().toString(); index++; } index = 0;
//さて房を行ってください.. while ( (line = reader.readLine()) != null && pos < maxRecords) { st = new StringTokenizer(line, DELIMITER); while (st.hasMoreElements()) { if (index == data[0].length) break; data[pos][index] = st.nextElement().toString(); index++; } index = 0; pos++;
if (pos == data.length - 1) { //もし必要ならばサイズ配列。 temp = new String[data.length + numToRead][size]; for (int n = 0; n < data.length; n++) { System.arraycopy(data[n], 0, temp[n], 0, temp[n].length); } data = temp; } } //配列を正確に分類してください..それは大きすぎますかもしれません temp = new String[pos][size]; //読まれたサイズ for (int n = 0; n < temp.length; n++) { System.arraycopy(data[n], 0, temp[n], 0, temp[n].length); } reader.close();
//ArrayListへストリングを交換します。 for (int i = 0; i < temp.length; i++) { arrlist = new ArrayList(); for (int j = 0; j < temp[i].length; j++) { arrlist1 = new ArrayList(); arrlist1.add(temp[i][j]); } arrlist1=null; arrlist.add(arrlist1); } return arrlist; }
/** * 方法はCSVファイルを読むことができ、ArrayListを返します。 * @param fullPathFilename CSVファイルのディレクトリー * @return arrlist * @throws IOException */ public ArrayList readCSV(String fullPathFilename) throws IOException{ String line; StringTokenizer st; int size = 0; int index = 0; int pos = 1; //第1個の位置を越える String[][] data; String[][] temp; final int DEFAULT_SIZE = 250; final String DELIMITER = ","; int maxRecords; int numToRead=0; int chr,k=0; StringBuffer buf=new StringBuffer(); ArrayList arrlist = null; ArrayList arrlist1 = null;
//読み取りラインの数を計算します。 BufferedReader reader1 = new BufferedReader(new FileReader(fullPathFilename)); while ( (chr = reader1.read()) != -1) { buf.append( (char) chr); } if((buf.substring(k,k+1).equals("\n")==true)|| (k+1==buf.length())) numToRead++; //数組の容量の大きさを得る BufferedReader reader = new BufferedReader(new FileReader(fullPathFilename)); line = reader.readLine(); st = new StringTokenizer(line, DELIMITER); while (st.hasMoreElements()) { st.nextElement(); size++; }
//配列を分類する、場合、セットされないパラメーター、全ファイルを読む、 //要求されるようにそうでなければ止まります。
if (numToRead == 0) { numToRead = DEFAULT_SIZE; maxRecords = Integer.MAX_VALUE; } else { maxRecords = numToRead; }
data = new String[numToRead][size];
//配列を増すためにそれを再び行います。 st = new StringTokenizer(line, DELIMITER); while (st.hasMoreElements()) { data[0][index] = st.nextElement().toString(); index++; } index = 0;
//さて房を行ってください.. while ( (line = reader.readLine()) != null && pos < maxRecords) { st = new StringTokenizer(line, DELIMITER); while (st.hasMoreElements()) { if (index == data[0].length) break; data[pos][index] = st.nextElement().toString(); index++; } index = 0; pos++;
if (pos == data.length - 1) { //もし必要ならばサイズ配列。 temp = new String[data.length + numToRead][size]; for (int n = 0; n < data.length; n++) { System.arraycopy(data[n], 0, temp[n], 0, temp[n].length); } data = temp; } } //配列を正確に分類してください..それは大きすぎますかもしれません temp = new String[pos][size]; //読まれたサイズ for (int n = 0; n < temp.length; n++) { System.arraycopy(data[n], 0, temp[n], 0, temp[n].length); } reader.close();
//ArrayListへストリングを交換します。 for (int i = 0; i < temp.length; i++) { arrlist = new ArrayList(); for (int j = 0; j < temp[i].length; j++) { arrlist1 = new ArrayList(); arrlist1.add(temp[i][j]); System.out.println("temp[i][j] = " + j +" ="+temp[i][j]); } arrlist1=null; arrlist.add(arrlist1); System.out.println("arrlist1 = " + i +" ="); } return arrlist; }

|