android應用私有存儲文件的寫入與讀取-openFileInput 和 openFileOutput


一:第一種方式就是像Java平台下的實現方式一樣通過構造器直接創建,如果需要向打開的文件末尾寫入數據,可以通過使用構造器FileOutputStream(File file, boolean append)將 append設置為true來實現。不過需要注意的是采用這種方式獲得FileOutputStream 對象時如果文件不存在或不可寫入時,會拋出 FileNotFoundException 異常。

  二:第二種獲取 FileInputStream 和 FileOutputStream 對象的方式是調用 Context.openFileInput 和 Context.openFileOutput兩個方法來創建。除了這兩個方法外,Context對象還提供了其他幾個用於對文件操作的方法,如下所示


Context對象中文操作的API及說明

方法名 說明
openFileInput(String filename) 打開應用程序私有目錄下的的指定私有文件以讀入數據,返回一個FileInputStream 對象
openFileOutput

打開應用程序私有目錄下的的指定私有文件以寫入數據,返回一個FileOutputStream 對象,

如果文件不存在就創建這個文件。

fileList() 搜索應用程序私有文件夾下的私有文件,返回所有文件名的String數組
deleteFile(String fileName) 刪除指定文件名的文件,成功返回true,失敗返回false


在使用openFileOutput方法打開文件以寫入數據時,需要指定打開模式。默認為零,即MODE_PRIVATE。不同的模式對應的的含義如下:

openFileOutput方法打開文件時的模式

常量   含義
MODE_PRIVATE 默認模式,文件只可以被調用該方法的應用程序訪問
MODE_APPEND   如果文件已存在就向該文件的末尾繼續寫入數據,而不是覆蓋原來的數據。
MODE_WORLD_READABLE 賦予所有的應用程序對該文件讀的權限。
MODE_WORLD_WRITEABLE   賦予所有的應用程序對該文件寫的權限。


   下面通過一個小例子來說明Android平台下的文件I/O 操作方式,主要功能是在應用程序私有的數據文件夾下創建一個文件並讀取其中的數據顯示到屏幕的 TextView中,這個例子也比較簡單只有一個類。
先看一下運行后的效果吧。

  1. package jcodecraeer.com;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import org.apache.http.util.EncodingUtils;
  5. import android.app.Activity;
  6. import android.graphics.Color;
  7. import android.os.Bundle;
  8. import android.widget.TextView;
  9. public class Activity01 extends Activity{
  10. //常量,為編碼格式
  11. public static final String ENCODING = "UTF-8";
  12. //定義文件的名稱
  13. String fileName = "test.txt";
  14. //寫入和讀出的數據信息
  15. String message = "歡迎大家來www.jcodecraeer.com";
  16. TextView textView;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. writeFileData(fileName, message);
  22. String result = readFileData(fileName);
  23. textView = (TextView)findViewById(R.id.tv);
  24. textView.setTextColor(Color.GREEN);
  25. textView.setTextSize(20.0f);
  26. textView.setText(result);
  27. }
  28. //向指定的文件中寫入指定的數據
  29. public void writeFileData(String filename, String message){
  30. try {
  31. FileOutputStream fout = openFileOutput(filename, MODE_PRIVATE);//獲得FileOutputStream
  32. //將要寫入的字符串轉換為byte數組
  33. byte[] bytes = message.getBytes();
  34. fout.write(bytes);//將byte數組寫入文件
  35. fout.close();//關閉文件輸出流
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. //打開指定文件,讀取其數據,返回字符串對象
  41. public String readFileData(String fileName){
  42. String result="";
  43. try {
  44. FileInputStream fin = openFileInput(fileName);
  45. //獲取文件長度
  46. int lenght = fin.available();
  47. byte[] buffer = new byte[lenght];
  48. fin.read(buffer);
  49. //將byte數組轉換成指定格式的字符串
  50. result = EncodingUtils.getString(buffer, ENCODING);
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. return result;
  55. }
  56. }

 http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0714/1437.html

 


免責聲明!

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



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