Android中的檔案儲存體資料方式

來源:互聯網
上載者:User

標籤:

轉自以下文章:

http://blog.csdn.net/zuolongsnail/article/details/6559338

http://blog.csdn.net/xiazdong/article/details/7687439

http://www.cnblogs.com/feisky/archive/2011/01/05/1926177.html

 

 

1.檔案儲存體資料使用了Java中的IO操作來進行檔案的儲存和讀取,只不過Android在Context類中封裝好了輸入資料流和輸出資料流的擷取方法。
建立的隱藏檔儲存在/data/data/<package name>/files檔案夾下。

 

 

2.操作。
儲存檔案內容:通過Context.openFileOutput擷取輸出資料流,參數分別為檔案名稱和儲存模式。
讀取檔案內容:通過Context.openFileInput擷取輸入資料流,參數為檔案名稱。
刪除檔案:Context.deleteFile刪除指定的檔案,參數為將要刪除的檔案的名稱。
擷取檔案名稱列表:通過Context.fileList擷取files目錄下的所有檔案名稱數組。
*擷取檔案路徑的方法:
絕對路徑:/data/data/<package name>/files/filename
Context:Context.getFilesDir()可以擷取到"/data/data/<package name>/files"

 

3.四種檔案儲存的模式。
Context.MODE_PRIVATE 為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下寫入的內容會覆蓋原檔案的內容。
Context.MODE_APPEND 檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。
MODE_WORLD_READABLE 表示當前檔案可以被其他應用讀取。
MODE_WORLD_WRITEABLE 表示當前檔案可以被其他應用寫入。
在使用模式時,可以用"+"來選擇多種模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);

 

 

 

簡單應用執行個體 1.

 

目標:當點擊儲存時,將以特定的檔案名稱和特定的檔案內容儲存內容,點擊讀取時,將讀取特定的檔案的檔案內容顯示到檔案內容文字框;

 

當點擊儲存之後,效果如下:

 

 

MainActivity.java

 

package org.xiazdong.file;    import java.io.ByteArrayOutputStream;  import java.io.FileInputStream;  import java.io.FileNotFoundException;  import java.io.FileOutputStream;    import android.app.Activity;  import android.content.Context;  import android.os.Bundle;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.EditText;    public class MainActivity extends Activity {      private Button saveButton,readButton;      private EditText filenameEt,filecontentEt;      private Context context = this;      private OnClickListener listener = new OnClickListener(){          @Override          public void onClick(View v) {              if(v==saveButton){                  String filename = filenameEt.getText().toString();                  String filecontent = filecontentEt.getText().toString();                  FileOutputStream out = null;                  try {                      out = context.openFileOutput(filename, Context.MODE_PRIVATE);                      out.write(filecontent.getBytes("UTF-8"));                  } catch (Exception e) {                      e.printStackTrace();                  }                  finally{                      try {                          out.close();                      } catch (Exception e) {                          e.printStackTrace();                      }                  }              }              else if(v==readButton){                  String filename = filenameEt.getText().toString(); //獲得讀取的檔案的名稱                  FileInputStream in = null;                  ByteArrayOutputStream bout = null;                  byte[]buf = new byte[1024];                  bout = new ByteArrayOutputStream();                  int length = 0;                  try {                      in = context.openFileInput(filename); //獲得輸入資料流                      while((length=in.read(buf))!=-1){                          bout.write(buf,0,length);                      }                      byte[] content = bout.toByteArray();                      filecontentEt.setText(new String(content,"UTF-8")); //設定文字框為讀取的內容                  } catch (Exception e) {                      e.printStackTrace();                  }                  filecontentEt.invalidate(); //重新整理螢幕                  try{                      in.close();                      bout.close();                  }                  catch(Exception e){}              }          }                };      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          saveButton = (Button)this.findViewById(R.id.saveButton);          readButton = (Button)this.findViewById(R.id.readButton);          filenameEt = (EditText)this.findViewById(R.id.filename);          filecontentEt = (EditText)this.findViewById(R.id.filecontent);          saveButton.setOnClickListener(listener);          readButton.setOnClickListener(listener);      }  }  

  

2、將檔案儲存到SDCard 

如果一個檔案很大,則不適用於存放在手機的儲存中;

如果手機存在sdcard,則sdcard的目錄為/mnt/sdcard目錄;

 

步驟1:設定模擬器支援sdcard 

 

此時模擬器已經支援sdcard了;

 

步驟2:在應用中設定許可權

 

在程式中訪問SDCard,你需要申請訪問SDCard的許可權。

在AndroidManifest.xml中加入訪問SDCard的許可權如下:

<!-- 在SDCard中建立與刪除檔案許可權 -->

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

<!-- 往SDCard寫入資料許可權 -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

要往SDCard存放檔案,程式必須先判斷手機是否裝有SDCard,並且可以進行讀寫。

注意:訪問SDCard必須在AndroidManifest.xml中加入訪問SDCard的許可權

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){         File sdCardDir = Environment.getExternalStorageDirectory();//擷取SDCard目錄         File saveFile = new File(sdCardDir, “a.txt”);       FileOutputStream outStream = new FileOutputStream(saveFile);         outStream.write("test".getBytes());         outStream.close();}Environment.getExternalStorageState()方法用於擷取SDCard的狀態,如果手機裝有SDCard,並且可以進行讀寫,那麼方法返回的狀態等於Environment.MEDIA_MOUNTED。Environment.getExternalStorageDirectory()方法用於擷取SDCard的目錄,當然要擷取SDCard的目錄,你也可以這樣寫:File sdCardDir = new File("/sdcard"); //擷取SDCard目錄File saveFile = new File(sdCardDir, "itcast.txt"); //上面兩句代碼可以合成一句: File saveFile = new File("/sdcard/a.txt");FileOutputStream outStream = new FileOutputStream(saveFile);outStream.write("test".getBytes());outStream.close();

  

 

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.