-4

How to read .txt file from drawable or any other folder in android using FileInputStream?

I have a .txt file in my drawable folder. How can i read the file and set it in Textview?

Ankush Kapoor
  • 445
  • 1
  • 8
  • 20

1 Answers1

3

Try This with help of this u can read file from drawable folder

        String data = "";
        StringBuffer sbuffer = new StringBuffer();
        InputStream is = this.getResources().openRawResource(+
                R.drawable.filetoread);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        if (is != null) {
            try {
                while ((data = reader.readLine()) != null) {
                    sbuffer.append(data + "\n");
                }
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         data = sbuffer.toString(),
        }
Vanraj Ghed
  • 1,261
  • 11
  • 23
  • Despite text files shouldn't be in the drawable folder as stated by @Abdul Fatir, this is useful to copy, for example, gif files that cannot be copied as a bitmap. – Juan Giorello Feb 07 '17 at 14:13