Can I read a local text file line by line into a string array?

南笙酒味 提交于 2019-12-21 02:55:16

问题


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

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"

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.

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.


回答1:


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

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

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).

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

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

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

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

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

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.

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()])

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.



来源:https://stackoverflow.com/questions/14968355/can-i-read-a-local-text-file-line-by-line-into-a-string-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!