2

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.

Yak Attack
  • 105
  • 2
  • 11
  • Take a look at the top 2 answers here for more help: http://stackoverflow.com/questions/4087674/android-read-text-raw-resource-file – dymmeh Feb 19 '13 at 22:09
  • Thanks a bunch! I will play with that and let you know if I get it working. – Yak Attack Feb 19 '13 at 22:23
  • But why can't you just add the text as a string variable in the strings xml resource file? here is the documentation for it: http://developer.android.com/guide/topics/resources/string-resource.html – Cata Feb 19 '13 at 22:25
  • The first few lines of text are short, but the last one could potentially be a pretty long string of characters. I suppose that would work, I just thought that this might be a little bit more elegant a solution than simply defining the strings in the XML. There may be as many as 100 of the text files defining certain information about different buildings. Depending on the selection from a list of buildings, the appropriate text file is loaded. I can still see that your simpler solution will work, but the other issue is that not everybody involved in the project knows how to work in the editor. – Yak Attack Feb 19 '13 at 23:01
  • continued... and I was hoping to get them to make all the text files, then I don't have to do all that work in the editor. Thanks for mentioning it though...I hadn't really considered it an option, but if all else fails, I suppose I can revert to that. – Yak Attack Feb 19 '13 at 23:03

1 Answers1

11

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.

Moog
  • 10,193
  • 2
  • 40
  • 66
  • 1
    Dude...bomb answer! I knew there must be something fundamentally wrong here, but even as I try to read about solutions, the language occasionally goes over my head. You are awesome. – Yak Attack Feb 21 '13 at 02:30