Android--持久化技術之檔案儲存體

來源:互聯網
上載者:User

標籤:

此乃本人的學習筆記   我只是將csdn部落格做為一個記錄學習的地方 So...

</pre><span style="font-size:18px;">1.通過openFileOutput()方法建立一個FileOutputStream對象out</span><p></p><p><span style="font-size:18px;">2.通過out new一個OutputStreamWriter對象</span></p><p><span style="font-size:18px;">3.通過OutputStreamWriter對象 new 一個BufferedWriter對象writer</span></p><p><span style="font-size:18px;">4.通過writer.write(data)將字串寫入檔案中  ps:data為字串類型</span></p><p></p><p><pre name="code" class="java"><span style="font-size:18px;">public void save(){  String datta="我是字串";  FileOutputStream out=null;  BufferedWriter writer=null;  try{    out=openFileOutput("data",Context.MODE_PRIVATE);    writer=new BufferedWriter(new OutputStreamWriter(out));    writer.write(data);  }catch(IOException e){     e.printStackTrace();  }finally{     try{       if(writer!=null){          writer.close();       }     }catch(IOException e){       e.printStackTrace();     }   }}</span>

openFileOutput()方法中有兩個參數。第一個為你指定的檔案名稱。該處檔案名稱不包括路徑(所有檔案預設儲存在/data/data/<packagename>/files/目錄下)。第二個參數為檔案的操作模式,有兩個可選MODE_PRIVATE和MODE_APPEND。MODE_PRIVATE為預設參數,表示當指定同樣的檔案名稱時,所寫入的內容將會覆蓋原檔案中的內容。MODE_APPEND表示如果該檔案存在就往檔案裡追加內容,否則就建立新檔案。

假如我們要儲存EditText中的資料,我們首先在onCreate()中獲得EditText的執行個體,在onDestory()方法中擷取EditText中的輸入內容,並調用save()方法把輸入的內容儲存到檔案。

====================================================================================================================================

writer一定要記得close()掉,不然資料根本存不進檔案 T_T

2016年5月9日

Android--持久化技術之檔案儲存體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.