简体   繁体   English

从资产文件夹中的文本文件加载数组(Android)

[英]Loading array from a text file in assets folder (Android)

PLEASE, 请,

I know this question has been asked many times, but I still can't get my code to work. 我知道这个问题已经问过很多遍了,但是我仍然无法使我的代码正常工作。

I have a text file in my assets folder called words.txt. 我的资产文件夹中有一个名为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. 我想将每个单词都放在一个数组中。

First I tried using scanner, but after reading many stackoverflow threads I found out I needed to use AssetManager. 首先,我尝试使用扫描仪,但是在阅读了许多stackoverflow线程后,我发现我需要使用AssetManager。

Here's what I tried: 这是我尝试过的:

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

It gives me the error message: default constructor cannot handle exception type IOException thrown by implicit super constructor... before I even do anything else. 它给了我错误消息: default constructor cannot handle exception type IOException thrown by implicit super constructor...在执行其他任何操作之前。 I don't know what this means. 我不知道这是什么意思。

Also, looking at the api, it seems like InputStream can only read bytes. 另外,看一下api,似乎InputStream只能读取字节。 How can I read words from the text file? 我如何从文本文件中读取单词?

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. 线程不堪重负,因为似乎每个答案都提出了不同的方法,使用InputStreams,BufferedStreams,FileInputStreams,File,Res文件夹,Asset文件夹以及我不熟悉的许多其他语言。 I am just learning to develop android apps and have limited java experience. 我正在学习开发android应用程序,并且Java经验有限。

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();
    }

Use this: 用这个:

Resources rs = getResources();
InputStream inputStream = rs.openRawResource(R.raw.sample);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
while ((line = r.readLine()) != null) {
    array.append(line);
}

You can use xml for loading the words like below: 您可以使用xml来加载如下所示的单词:

res/values/strings.xml: RES /值/ strings.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>
</resources>

In class file: 在课程档案中​​:

Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Source 资源

put you array inside res/values/strings.xml as 将数组放在res / values / strings.xml中作为

 <string-array name="spinnerNames">
    <item>Trust</item>
    <item>Grade</item>
    <item>Location</item>
    <item>Contract Type</item>
    <item>Speciality</item>

</string-array>

and load your array as: 并将您的数组加载为:

 String[] spinnerNames = getResources().getStringArray(R.array.spinnerNames);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM