例如參考書目檔books.csv如下:
Professional Android Sensor Programming,Wiley,2012,Greg Milette,978-1-118-18348-9
Android初學特訓班,碁峯,2011,鄧文淵,978-986-276-397-1
Android 2.X應用程式開發實戰,碁峯,2011,林城,978-986-276-142-7
深入淺出Android遊戲程式開發範例大全,博碩,2011,吳亞峰,978-986-201-410-3
Pro Android 4,Apress,2012,Satya Komatineni,978-143-023-930-7
Beginning Android 4 Games Development,Apress,2011,Mario Zechner,978-143-023-987-1
深入淺出Android專題實作,博碩,2011,郭宏志,978-986-201-449-3
Android SDK開發範例大全 (第3版),悅知,2012,余志龍,978-986-607-200-0
針對CSV檔案,較方便的做法是透過BufferedReader.readLine()方法每次讀取單行資料,再使用String.split()方法依據分隔字元將資料行分割為資料欄。
private void readCSVfile(){
String sdcardDir = Environment.getExternalStorageDirectory().toString();
String csvfile = sdcardDir+"/books.csv";
StringBuilder sb = new StringBuilder ("References:\n");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(csvfile)));
String data = "";
while ((data = br.readLine()) != null) {
String[] sarray = data.split(",");
sb.append(sarray[0] + "\n"); //title
sb.append("isbn: " + sarray[4] + "\n"); //isbn
sb.append("publisher: " + sarray[1] + " " + sarray[2] + "\n\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txtview.setText(sb.toString());
}