title: Access resource files in Android
body: <p>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.</p>
  <pre><code>public static void main(String[] args) {
  
      File file = new File(&quot;res/raw/textfile.txt&quot;);
      
      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(&quot;GAME&quot;, dis.readLine()); 
        }
  
        fis.close();
        bis.close();
        dis.close();
  
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  </code></pre>
  <p>I have tried different path syntax but always get a <strong>java.io.FileNotFoundException</strong> error. How can I access /res/raw/textfile.txt for processing? Is <strong>File file = new File(&quot;res/raw/textfile.txt&quot;);</strong> the wrong method in Android?</p>
  <hr />
  <p>***** Answer: *****</p>
  <pre><code>// Call the LoadText method and pass it the resourceId
  LoadText(R.raw.textfile);
         
  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(&quot;TEXT&quot;, readLine);
          }
  
          // Close the InputStream and BufferedReader
          is.close();
          br.close();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>Note this will return nothing, but will print the contents line by line as a <code>DEBUG</code> string in the log.</p>