您可以从SD卡读取一个平面文件,每行插入1个语句并遍历该文件.
这是一个类似的例子,我在其中读取文件并将ContactItem添加到我的contact_list:
public ArrayList readInputFile(Context context, String filename) {
String text = null;
BufferedReader reader = null;
ArrayList contact_list = new ArrayList();
contact_list.clear(); // Clear any existing contacts when a new file is read
try {
reader = new BufferedReader(new FileReader(filename));
// Repeat until EOF
while (((text = reader.readLine()) != null)) {
if (!(text.length() > 1024)) { // If we read more than 1k per line there's a problem with the input file format
ContactItem c = new ContactItem(text);
if (c.getIsValid()) {
// We were able to parse a well formed phone number from the last line read
contact_list.add(c);
}
}
}
} catch (FileNotFoundException e) {
Toast.makeText(context, R.string.error_fileNotFound, 1).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(context, R.string.error_ioException, 1).show();
e.printStackTrace();
} finally { // EOFException (or other, but EOF is handled and most likely)
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
Toast.makeText(context, R.string.error_generalFailure, 1).show();
e.printStackTrace();
}
}
return contact_list;
}