tags:

views:

83

answers:

1

I am trying to read a file "words.txt" from a resource. It is a very simple, but large (2 MB), text file that I want to read line by line. I have put the file into /res/raw/words.txt, and try to open it with the following code:

    try 
    {   
        BufferedReader in = 
            new BufferedReader(
            new InputStreamReader(getResources().openRawResource(R.raw.words)));
        String line=in.readLine();
        T.append(line); T.append("\n");
        in.close();
    }
    catch (Exception e) { T.append(e.toString()); }

However, I get a java.io.IOException. This is not a "resource not found" exception, so the resource is opened correctly, but the readLine() produces the error.

I tried using the InputStream itself, with the result that read() produces -1, which stands for EOF, as if the file was empty.

Any help for me?

A: 

Try this:

InputStream is = c.getResources().openRawResource(R.raw.csv_file);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String readLine = null;

        try {
            while ((readLine = br.readLine()) != null) {


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
Sameer Segal
`c` i presume is the `Context`...
st0le
Yes, sorry about the confusion - works?
Sameer Segal
No, does not work. I would be surprised anyway, since the resource is found and opened correctly. Only, that the system pretends it is empty.
Rene
But I found the reason. I shortened the file, and suddenly, it was working. Now I have to find out, if that is a restriction of Android, a restriction or setting of Eclipse, or a problem with the file itself.
Rene
I have split the file in 3 and everything works now. Why? I do not know yet.
Rene
You might want to download the file to the SD Card and read it (through the same code)
Sameer Segal