• 10

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Access resource files in Android

I have a resource file in my /res/raw/ folder (/res/raw/textfile.txt) which I am trying to read from my android app for processing.

public static void main(String[] args) {

    File file = new File("res/raw/textfile.txt");

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;

    try {
      fis = new FileInputStream(file);
      bis = new BufferedInputStream(fis);
      dis = new DataInputStream(bis);

      while (dis.available() != 0) {
              // Do something with file
          Log.d("GAME", dis.readLine()); 


    } catch (FileNotFoundException e) {
    } catch (IOException e) {

I have tried different path syntax but always get a java.io.FileNotFoundException error. How can I access /res/raw/textfile.txt for processing? Is File file = new File("res/raw/textfile.txt"); the wrong method in Android?

* Answer: *

// Call the LoadText method and pass it the resourceId

public void LoadText(int resourceId) {
    // The InputStream opens the resourceId and sends it to the buffer
    InputStream is = this.getResources().openRawResource(resourceId);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String readLine = null;

    try {
        // While the BufferedReader readLine is not null 
        while ((readLine = br.readLine()) != null) {
        Log.d("TEXT", readLine);

    // Close the InputStream and BufferedReader

    } catch (IOException e) {

If you have a file in res/raw/textfile.txt from your Activity/Widget call:

getResources().openRawResource(...) returns an InputStream

The dots should actually be an integer found in R.raw... corresponding to your filename, possibly R.raw.textfile (it's usually the name of the file without extension)

new BufferedInputStream(getResources().openRawResource(...)); then read the content of the file as a stream

  • 32
Reply Report
      • 1
    • No matter how I use "getResources().openRawResource(R.raw.textfile)" eclipse always gives the error "The method getResources() is undefined for the type MyClass".
      • 1
    • I've tried: File file = new File(R.raw.textfile); - I'll try to use getResources().OpenRawResource(R.raw.textfile) and give that to File if I can.