我想獲得一個簡單的Hello World txt文件,然後通過我的android應用程序讀取。在查看DDMS文件資源管理器時,它成功地創建了文本文件,但是當我嘗試讀取它時,我得到一個FileNotFoundException。Android模擬器寫入文件,但無法找到目錄
try {
final String TESTSTRING = new String("Hello World");
FileOutputStream 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();
}
也是錯誤的是java.io FileNotFoundException: /test.txt (No such file or directory)
任何幫助感謝。
'openFileOutput()'目錄保存其文件由'getFilesDir()'標識。 OP應該按照你的建議使用'openFileInput()'或者使用'getFilesDir()'來構建一個有效的'File'對象到正確的位置,以便能夠直接使用'FileInputStream'。 – CommonsWare 2011-04-16 19:24:42