在 Android Studio 中加载一个简单的文本文件 [英] Load a simple text file in Android Studio

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

问题描述

获得了一个使用 Google 新 Android Studio IDE 的全新项目.

Got a brand new project using Google's new Android Studio IDE.

我正在尝试使用 InputStreamReader 加载一个简单的文本文件.我收到一个找不到文件的异常.现在没有任何 assets/文件夹.我试图创建一个并在许多不同的位置添加我的文件(在项目的根目录,在 .java 文件的根目录等......)我试图移动文件但仍然没有得到文件找到了.

I'm trying to load a simple text file using an InputStreamReader. I'm getting a file not found exception. Now there isn't any assets/ folder. I tried to create one and add my file at many different spots (at the root of the project, at the root of the .java file, etc...) I've tried to move the file around but still get the file not found.

现在使用 Eclipse 从来没有问题,因为任何模板都可以创建一个资产文件夹.

Now that never was a problem using Eclipse as there is an assets folder created by any template.

有谁知道资产应该去哪里或如何加载它们?

Does anyone know where should the assets go to or how to load them?

这是使用的代码,它在 .open() 处失败:

Here is the code used, it fails at .open():

InputStream iS = resources.getAssets().open("bla.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(iS));

我也在 E​​clipse 中尝试了这段代码,它可以工作并且文件内容被加载.因此,Android Studio 中可能需要一个步骤.

I also tried this code in Eclipse, it works and the file contents get loaded. So there's probably a step needed in Android Studio.

推荐答案

  1. 第 1 步:在 Name_Project-Name_Project.iml 文件中打开.
  2. 见行:选项名称="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets"
  3. 第 2 步:在主文件夹中创建一个子文件夹assets".
  4. 第 3 步:将文件放在此文件夹中.
  5. 第 4 步:加载它.完毕.

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

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