-3
我有某幾個txt文件already.I想讀我application.i點擊此按鈕,文件和選擇TXT文件的名稱和閱讀。我該怎麼辦呢? 請幫助我。從txt文件中的Android移動設備
我有某幾個txt文件already.I想讀我application.i點擊此按鈕,文件和選擇TXT文件的名稱和閱讀。我該怎麼辦呢? 請幫助我。從txt文件中的Android移動設備
你可以把文件中的謨的資產目錄,並使用
AssetManager am = context.getAssets();
我覺得這個鏈接可以幫助你:http://www.technotalkative.com/android-read-file-from-assets/
您可以使用下面的代碼清單讀取文本的內容文件。
爲了獲取路徑:
File file = app.getFilesDir();
String path = file.getAbsoluteFile().getAbsolutePath() + "<filename.extension>";
--------------------------------------------------------
public static String readAllContents(String path) {
String fileContents = null;
try {
InputStream inputStream = new FileInputStream(path);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
fileContents = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("exception", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("exception", "Can not read file: " + e.toString());
}
return fileContents;
}
這個問題可能是重複的http://stackoverflow.com/questions/12421814/how-to-read-text-file-in-android的 – snehal