从资产的文件夹中的文本文件加载阵列(Android版) [英] Loading array from a text file in assets folder (Android)

查看:99
本文介绍了从资产的文件夹中的文本文件加载阵列(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了很多次,但我还是不能让我的code工作。

I know this question has been asked many times, but I still can't get my code to work.

我在叫words.txt资产文件夹中的文本文件。有每行一个词在文本文件中的每一行,没有空行。我希望把每一个字在数组中。

I have a text file in my assets folder called words.txt. There is one word per line in each line of that text file with no blank lines. I want to put every word in an array.

首先,我尝试使用扫描仪,但阅读许多计算器线程后,我发现我需要使用AssetManager。

First I tried using scanner, but after reading many stackoverflow threads I found out I needed to use AssetManager.

下面是我的尝试:

AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("words.txt");

这给我的错误信息:默认构造函数不能处理由隐超级构造函数抛出的异常类型IOException异常... 之前,我连做别的。我不知道这意味着什么。

It gives me the error message: default constructor cannot handle exception type IOException thrown by implicit super constructor... before I even do anything else. I don't know what this means.

此外,望着API,好像InputStream中只能读取的字节。如何从文本文件中读取单词?

Also, looking at the api, it seems like InputStream can only read bytes. How can I read words from the text file?

线程是压倒性的,因为它看起来像每个答案提出了不同的方法,用InputStreams,BufferedStreams,FileInputStreams,文件,文件夹的RE,资产文件夹和许多其他语言,我不熟悉。我刚学来开发Android应用,并有有限的Java经验。

The threads are overwhelming because it seems like each answer proposes a different method, using InputStreams, BufferedStreams, FileInputStreams, File, Res folders, Asset folders, and lots of other language I am unfamiliar with. I am just learning to develop android apps and have limited java experience.

推荐答案

如下图所示你可以这样做:

You can do as shown below:

    try {
        BufferedReader bReader = new BufferedReader(new InputStreamReader(getAssets().open("file.txt")));
        ArrayList<String> values = new ArrayList<String>();
        String line = bReader.readLine();
        while (line != null) {
            values.add(line);
            line = bReader.readLine();
        }
        bReader.close();
        for (String v : values)
            Log.i("Array is ", v);
    } catch (IOException e) {
        e.printStackTrace();
    }

这篇关于从资产的文件夹中的文本文件加载阵列(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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