读一个简单的文本文件 [英] Reading a simple text file

查看:157
本文介绍了读一个简单的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读我的样本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.

感谢您的帮助

推荐答案

将文本文件中的 /资产目录下的Andr​​oid项目下。使用 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);

这篇关于读一个简单的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆