讀取 csv 檔 ,輸出成 ArrayList
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ReadCSVtoArrayList { /** * Read csv file * @param csvpath path of csv file * @return ArrayList, element is data of column */ public static ArrayList<ArrayList> readCSVToArrayList(String csvpath) { ArrayList<ArrayList> dataAL = new ArrayList<ArrayList>(); BufferedReader reader; try { reader = new BufferedReader(new FileReader(csvpath)); reader.readLine();// read first line in csv String line = null; while ((line = reader.readLine()) != null) { ArrayList<String> ticketStr = new ArrayList<String>(); String item[] = line.split(","); ticketStr.clear(); for(int i=0; i<item.length; i++){ ticketStr.add(i, item[i]); } dataAL.add(ticketStr); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return dataAL; } }
接著就能操作 ArrayList 中的資料,完成需要的功能
哭爹喊娘
12 3 月, 2014 1:32 下午不好意思
我想請問一下
如果我現在有一個data.csv的檔案
我要怎麼去那個檔案的路徑去把他讀出來?