0

Hi everyone I am trying to read the date from a file in android. I am using Eclipse and the program is compiling and running, just it is not printing the context of the txt file. Here is my load method

    private String load(String filename) {
        try {
//          Log.v("Home", " in the load method");
            Log.d("Home", " in the load method");

            final FileInputStream fis = openFileInput(filename);
//          final InputStream fis =  getResources().openRawResource(R.raw.pages);

            final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            fis.close();
            return sb.toString();
        } catch (Exception ex) {
            return "No entry exists for this file";
        }
    }

and in the oncreate i just access it

String fileName = "pages.txt";
load(fileName);

pages.txt is in the res/raw directory. I tried to read the file with both

final FileInputStream fis = openFileInput(filename);
// final InputStream fis =  getResources().openRawResource(R.raw.pages);

but It is not printing the context.

I added in the onCreate method

Log.d("File", load(fileName));

but is returning the catch statement No entry exists for this file.

Thanks

Doesn't Matter
  • 405
  • 4
  • 12
  • 23
  • 8
    You're currently ignoring the return value of the `load` method. How do you expect that to work? – Jon Skeet Dec 24 '12 at 17:48
  • Resources must be dealt with via `getResources()`, there is no file named `"pages.txt"` anywhere in the package. Stick to `openRawResource`... – K-ballo Dec 24 '12 at 17:50
  • http://stackoverflow.com/a/5675934/1339473 this link may help you i think – QuokMoon Dec 24 '12 at 18:06

1 Answers1

0

try to use like below,

System.out.println(load(raw.pages));



 private String load(int id) {
        try {           
             Resources res = getResources();
             InputStream fis = res.openRawResource(id);
            final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            fis.close();
            return sb.toString();
        } catch (Exception ex) {
            return "No entry exists for this file";
        }
    }
Talha
  • 12,673
  • 5
  • 49
  • 68