开发者

Android Emulator writes file but cannot find directory

开发者 https://www.devze.com 2023-02-27 00:02 出处:网络
I am trying to get a simple Hello World txt file to be written then read by my android application. When viewing the DDMS File Explorer it successfully creates the text file but i then get a FileNotFo

I am trying to get a simple Hello World txt file to be written then read by my android application. When viewing the DDMS File Explorer it successfully creates the text file but i then get a FileNotFoundException when trying to read it.

try {

        final String TESTSTRING = new String("Hello World");

        Fi开发者_StackOverflowleOutputStream fOut = openFileOutput("test.txt", MODE_WORLD_READABLE);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        osw.write(TESTSTRING);
        osw.flush();
        osw.close();

        FileInputStream fIn = new FileInputStream("test.txt");
        InputStreamReader isr = new InputStreamReader(fIn);

        char[] inputBuffer = new char[TESTSTRING.length()];

        isr.read(inputBuffer);

        String readString = new String(inputBuffer);

        boolean isTheSame = TESTSTRING.equals(readString);

        Log.i("File Reading Stuff", "success = " + isTheSame);

    } catch (IOException e) {
        e.printStackTrace();
    }

also the error is java.io FileNotFoundException: /test.txt (No such file or directory)

Any Help Thanks.


I don't know where openFileOutput saves its files, but wouldn't you use it's input equivalent openFileInput to read such a file?


See my previous post with information about how to read/write to the external storage directory in Android:

Android how to use Environment.getExternalStorageDirectory()

-- Dan

0

精彩评论

暂无评论...
验证码 换一张
取 消