1)// 在手機中創建文件
FileOutputStream phone_outStream =this.openFileOutput("1.txt", Context.MODE_PRIVATE);
phone_outStream.write("HELLO".getBytes());
FileOutputStream phone_outStream1 =openFileOutput("1.txt", Context.MODE_APPEND); //追加模式繼續寫
phone_outStream1.write(" world".getBytes());
//讀取並顯示
byte[] s= new byte[80];
FileInputStream phone_inputStream =openFileInput("1.txt");
phone_inputStream.read(s);
Toast.makeText(this, new String(s), Toast.LENGTH_SHORT).show();
結論:不管手機data文件夾是否能在DDMS中看到東西, (沒有root權限就會空空如也.)程序能夠正常運行,並toast出正確內容.
2)如果試圖用該方法來寫入到手機內部存儲中,是不行的:
java.io.FileNotFoundException: /2.txt: open failed: EROFS (Read-only file system)
File f = new File("2.txt");
FileOutputStream fs = new FileOutputStream( f ); //這句導致異常
openFileOutput();是android的api, android.content.ContextWrapper.openFileOutput();
FileOutputStream .是Java的類. java.io.FileOutputStream