[英]How to choose a txt file from raw folder, get path and read data?
I have a big text file of about 40mbs, I have been looking for a way to read its contents and I found this:我有一个大约 40mbs 的大文本文件,我一直在寻找一种方法来读取它的内容,我发现了这个:
File f = new File(String.valueOf(getResources().openRawResource(R.raw.test)));
try {
FileInputStream inputStream = new FileInputStream(f);
Scanner sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Unfortunately I am getting the following error:不幸的是我收到以下错误:
java.io.FileNotFoundException: android.content.res.AssetManager$AssetInputStream@ead83c (No such file or directory)
The file I am trying to access is in raw folder, and its name is test.txt我试图访问的文件位于原始文件夹中,其名称为test.txt
Where am I getting it wrong?我哪里弄错了?
Resources.openRawResource
already gives you an InputStream
. Resources.openRawResource
已经为您提供了InputStream
。
final InputStream inputStream = getResources().openRawResource(R.raw.test);
try {
Scanner sc = new Scanner(inputStream, "UTF-8");
// ...
} finally {
inputStream.close();
}
Raw resources are packed inside the APK.原始资源打包在 APK 中。 They're not accessible using a file system path you're used to.
使用您习惯的文件系统路径无法访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.