我已经有几个 txt 文件。我想在我的应用程序中读取这些文件。我单击按钮并选择 txt 文件的名称并读取它。我怎么做?请帮助我。
问问题
393 次
2 回答
0
可以把文件放到项目的asset目录下使用
AssetManager am = context.getAssets();
我认为这个链接可以帮助你:http ://www.technotalkative.com/android-read-file-from-assets/
于 2013-07-24T07:59:10.060 回答
0
您可以使用以下代码清单来读取文本文件的内容。
获取路径:
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;
}
于 2013-07-24T09:16:01.127 回答