Android寫入文件到手機.


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM