我可以阅读本地文本文件中按行放入一个字符串数组?数组、字符串、文本文件、放入

2023-09-05 11:30:26 作者:酒醉人不归

这个问题:如何阅读一本地(RES / RAW)文件一行行?有类似问题的处理,但我无法构造上只要有答案的解决方案的基础。我没有得到的信息(其中有一个ReadLine方法DataInputStream类)是非常​​有效的,而且我一直在研究这个的开发者网站,并试图使其工作。

The question "How to read a local (res/raw) file line by line?" deals with a similar issue but I was unable to construct a solution base on the answers provided there. I did get a very useful piece of info (the DataInputStream class which has a readLine method), and I have been researching this on the developer website and trying to make it work.

我所要做的是存储在文本文件中的连续行成一个字符串数组读取信息,使得第一行是第一个数组元素,第二行是下一个数组元素,等...和然后这个字符串数组将被用于填充在打开的下一个活动的文本字段。这一切都发生的开关盒内(取决于选择哪个列表项的情况下,即,不同的文本文件加载)。 这是我到目前为止有:

What I am trying to do is read information stored in successive lines of a text file into a string array, such that the first line is the first array element, the second line is the next array element, etc... and then this string array is going to be used to populate text fields in the next activity that is opened. This is all happening inside of a switch case (depending on the case i.e. which list item is selected, a different text file is loaded). Here is what I have so far:

//Retrieve necessary text file for inputstream
InputStream buildinginfo = getResources().openRawResource(R.raw.testbuilding);
class DataInputStream extends FilterInputStream{
    protected DataInputStream(InputStream buildinginfo) {
        super(buildinginfo);
        // TODO Auto-generated constructor stub
        int i;  
        String[] building_info;
        //Assign lines of text to array
        for(i=0; i<5; i++){
            building_info[i] = buildinginfo.readLine();
        }
    }
}

到目前为止,编辑器是好的,它除了这些错误,我没有足够的经验来理解它们。我知道他们说了什么,但不是如何解决这些问题。该错误是在开关情况下,我想设置输入流,并指定值内的部分。 最重要的是,在readline的命令发生了行了,我得到: - 该方法的readLine是未定义类型的DataInputStream - 该方法的readLine是未定义的类型InputStream的

So far the editor is okay with it except for these errors, and I am not experienced enough to make sense of them. I understand what they are saying but not how to fix them. The errors are in the section inside the switch case where I am trying to set up the input stream and assign the values. Most importantly, in the line where the readLine command takes place, I get: "- The method readLine is undefined for the type DataInputStream" "- The method readLine is undefined for the type InputStream"

这我不明白,因为如果我没有记错,这里说的( http://developer.android.com/reference/java/io/DataInputStream.html )是DataInputStream类具有可ReadLine方法(我发现这个从上面提到的问题)。很显然,我没有用过的DataInputStream正确,但我似乎无法弄清楚如何。我已经通过在这里的几个问题,看了一下,上面提到多次的链接页面。

This I do not understand because if I am not mistaken, it says here (http://developer.android.com/reference/java/io/DataInputStream.html) that the DataInputStream class has the readLine method available (I found out about this from the question referred to above). Obviously I have not used the DataInputStream correctly, but I can't seem to figure out how. I have looked through several questions on here and referred to the page linked above several times.

如果任何人都可以看到我在做什么错了,我会AP preciate您的帮助很大。如果我找错了树完全是为了这类型的任务,我浪费时间道歉,但一些指导或转诊到合适的教程资源将大大AP preciated。我花了最后两天,试图找出这些错误。

If anybody can see what I am doing wrong I would appreciate your help very much. If I am barking up the wrong tree entirely for this type of task, I apologize for wasting time, but some guidelines or a referral to an appropriate tutorial resource would be much appreciated. I have spent the last two days trying to figure out these errors.

推荐答案

好了,所以没有人曾在这里帮你了...这里去。

Okay, so nobody else has helped you out here ... here goes.

实际上你已经在你如何使用API​​类的理解是错误的一步。

Essentially you've made a wrong turn in your understanding of how to use API classes.

您code试图为确定 DataInputStream类。你想用一个已经提供的API,而不是一个。通过重新定义类,你实际上是掩盖了API类(你绝对不希望出现这种情况)。

Your code is attempting to define the DataInputStream class. You want to use the one that is already provided by the API instead. By redefining the class you are actually masking the API class (you definitely don't want that).

所以,如果你对的DataInputStream的文档,你会看到一个构造函数。这使您可以创建一个类的例如。这是你想要什么,而不是:)

So if you look at the documentation for DataInputStream you'll see a constructor. This allows you to create an instance of a class. This is what you want instead :)

InputStream buildinginfo = getResources().openRawResource(R.raw.testbuilding);
DataInputStream myDIS = new DataInputStream(buildinginfo);

//you've now got an instance of DataInputStream called myDIS

String firstString = myDIS.readLine();

//voila ... your firstString variable should contain some text

另外还有一个问题,你数组code ......但我不会以这种方式使用数组。

There's also an issue with you array code ... but I would not use an array in this way.

您应该使用ArrayList中,然后读入while循环的线,所以你并不需要修补的大小。

You should use ArrayList, then read the lines in a while loop so you don't need to tinker with the size.

首先,让我们摆脱这一行的(我将其注释掉:

First let's get rid of this line (I'll comment it out:

//String firstString = myDIS.readLine();

和创建一个ArrayList是一个模板类,注意 String的实例中尖括号表示什么样的元素,我们希望在我们的数组:

And create an instance of an ArrayList which is a template class so note the String in the angle brackets denotes what sort of elements we want in our array:

ArrayList<String> list = new ArrayList<String>();

您可以使用Add方法将元素添加到ArrayList。 我们要做整个事情一气呵成,不用担心,我会解释后...

You can use the add method to add elements to the arraylist. We're going to do the whole thing in one go, don't worry I'll explain after ...

String myLine; //declare a variable

//now loop through and check if we have input, if so append it to list

while((myLine=myDIS.readline())!=null) list.add(myLine);

最后一行是有点嚼劲,但是当你分配MYLINE这个变量返回一个结果,你可以使用NULL比较......一旦最后一行被读取后,readline的函数将返回null。 while循环的条件等同于虚假和滴出循环。

The last line is a bit chewy but when you assign the myLine variable this returns a result which you can compare with NULL ... once the last line is read, the readline function will return null. The condition of the while loop equates to false and drops out of the loop.

有一个叫一个简单的辅助函数的toArray 来把你的数组列表变成一个简单的数组。

There is a simple helper function called toArray to turn your array list into a simple array.

String[] myStringArray = list.toArray(new String[list.size()])

您现在所需要的输出。我建议看在Android上一些影片获得手感一般的语言,但我希望这有助于你inderstand那里的错误了。

You now have the desired output. I would suggest watching some videos on Android to get a general feel for the language, but I hope this helped you inderstand where the mistake was.