2

私は困惑している。このスレッドを読んだ後、 List を内部ストレージに書き込んでから、 List をonPause()読み取ろうとしていonResume()ます。テスト目的で、例が示すように単純な文字列を記述しようとしています。私はこれを書くために持っています:

    String FILENAME = "hello_file";
    String string = "hello world!";

    FileOutputStream fos = null;
    try {
        fos = openFileOutput(FILENAME, Context. MODE_WORLD_READABLE);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        fos.write(string.getBytes());
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

そして、私はonResume()呼び出してそれを読み込もうとしています:

    try {
            resultText01.setText(readFile("hello_file"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            resultText01.setText("exception, what went wrong? why doesn't this text say hello world!?");
        }

もちろんこれを使用します:

private static String readFile(String path) throws IOException {
      FileInputStream stream = new FileInputStream(new File(path));
      try {
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
        return Charset.defaultCharset().decode(bb).toString();
      }
      finally {
        stream.close();
      }
    }

何らかの理由で、resultText01.setText(...)に設定されておらず"hello world!"、代わりに catch 例外を呼び出しています。私の専門用語が正しくない場合は申し訳ありませんが、私はこれが初めてです。ご指摘ありがとうございます。

4

1 に答える 1

2

メソッドの次の代わりにreadFile(...)...

FileInputStream stream = new FileInputStream(new File(path));

試す...

FileInputStream stream = openFileInput(path);
于 2011-06-25T22:02:52.000 に答える