Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Can't Read From File using FileInputStream

I'm trying to read from a file called "quiz_questions.txt" in my res/raw folder. The code I have compiles, but it looks like it stops before it gets to the FileInputStream. Maybe it is opening it, but not reading it. I'm not sure.

import java.io.*;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;

public class Questions extends Activity {

public String[][] questions = new String[10][5];

public void fillArray() {
    {
        Context con = null;
        try {
            //fis = new BufferedInputStream(new FileInputStream("res/raw/quiz_questions.txt"));
            FileInputStream fis = (FileInputStream) con.getResources().openRawResource(R.raw.quiz_questions);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String nextLine;
            int i = 0, j = 0;
            while ((nextLine = br.readLine()) != null) {
                if (j == 5) {
                    j = 0;
                    i++;
                }
                questions[i][j] = nextLine;
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
}
like image 694
rockhard_ridefree Avatar asked Mar 25 '12 21:03

rockhard_ridefree


People also ask

What does the read () method of FileInputStream return?

The read() method of a FileInputStream returns an int which contains the byte value of the byte read. If the read() method returns -1, there is no more data to read in the stream, and it can be closed. That is, -1 as int value, not -1 as byte value.

Does FileInputStream need to be closed?

close() method. After any operation to the file, we have to close that file.

How do I use Fileoutputstream on Android?

Creates a file output stream to write to the file with the specified name. If the second argument is true , then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.


1 Answers

You don't post it, but I imagine that you are getting a NullPointerException. This is because con is null when you try to create the FileInputStream.

Since an Activity is already a Context, you can just eliminate con from the statement. (You should also use the InputStream interface instead of FileInputStream.)

InputStream is = getResources().openRawResource(R.raw.quiz_questions);

Finally, you should reorganize your code so is is closed whether or not an exception is thrown:

public void fillArray() {
    try (InputStream is = getResources().openRawResource(R.raw.quiz_questions)) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String nextLine;
        int i = 0, j = 0;
        while ((nextLine = br.readLine()) != null) {
            if (j == 5) {
                j = 0;
                i++;
            }
            questions[i][j] = nextLine;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
like image 88
Ted Hopp Avatar answered Sep 27 '22 17:09

Ted Hopp