我已经有了几个txt文件。我想在我的应用程序中读取这些文件.i点击按钮并选择txt文件的名称并阅读它。我是怎么做到的 请帮帮我。
答案 0 :(得分:0)
您可以将文件放在项目的资产目录中并使用
AssetManager am = context.getAssets();
我认为此链接可以为您提供帮助:http://www.technotalkative.com/android-read-file-from-assets/
答案 1 :(得分: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;
}