问题描述
我想读我的样本Android应用程序一个简单的文本文件。我现在用的是写在下面code读取简单的文本文件。
I am trying to read a simple text file in my sample Android Application. I am using the below written code for reading the simple text file.
1. InputStream inputStream = openFileInput("test.txt");
2. InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
3. BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
我的问题是:我应该在哪里放置在我的项目这的test.txt
文件?我试图把文件在RES /生
和资产
文件夹,但我得到的例外FileNotFound
时的code上面写的第一现场得到执行。
My questions is :Where should I place this "test.txt"
file in my project?. I have tried putting the file under "res/raw"
and "asset"
folder but I get the exception "FileNotFound"
when first live of the code written above gets executed.
感谢您的帮助
推荐答案
将文本文件中的 /资产
目录下的Android项目下。使用 AssetManager
类来访问它。
Place your text file in the /assets
directory under the Android project. Use AssetManager
class to access it.
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
或者,你也可以把文件中的 / RES /生
目录,其中的文件将被编入索引,是由R档的ID访问:
Or you can also put the file in the /res/raw
directory, where the file will be indexed and is accessible by an id in the R file:
InputStream is = getResources().openRawResource(R.raw.test);
这篇关于读一个简单的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!