0

大家好,我正在尝试从 android 中的文件中读取日期。我正在使用 Eclipse,程序正在编译和运行,只是它没有打印 txt 文件的上下文。这是我的加载方法

    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";
        }
    }

在 oncreate 我只是访问它

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

pages.txt 位于 res/raw 目录中。我试图用两者来读取文件

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

但它没有打印上下文。

我在 onCreate 方法中添加

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

但正在返回 catch 语句No entry exists for this file.

谢谢

4

1 回答 1

0

尝试像下面这样使用,

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";
        }
    }
于 2012-12-24T18:04:25.220 回答