歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android入門:File文件存儲

Android入門:File文件存儲

日期:2017/3/1 10:15:57   编辑:Linux編程

數據的存儲有多種方式,比如數據庫存儲、SharedPreferences存儲、文件存儲等;

這裡我們將要介紹最簡單的文件存儲方式;

文件存儲簡單的來說就是一般的JAVASE中的IO流,只是把他應用於Android手機中而已;

一、文件存儲核心代碼


文件存儲


(1)FileOutputStream out = context.openFileOutput(String filename,int mode); 以mode模式獲得文件輸出流

(2)out.write(byte[]b);

  1. FileOutputStream out = null;
  2. out = context.openFileOutput(filename, Context.MODE_***);
  3. out.write(filecontent.getBytes("UTF-8"));
  4. out.close();
注意:文件默認會存儲到/data/data/package/files中;

文件讀取


(1)FileInputStream in = context.openFileInput(String filename); 獲得某個文件的文件流

(2)int length = in.read(byte[]);

  1. /*
  2. 每次讀取固定的字節,並將此字節輸出到字節輸出流中,當全部讀取完畢後,將字節流中的內容一並輸出
  3. */
  4. FileInputStream in = null;
  5. ByteArrayOutputStream bout = null;
  6. byte[]buf = new byte[1024];
  7. bout = new ByteArrayOutputStream();
  8. int length = 0;
  9. in = context.openFileInput(filename); //獲得輸入流
  10. while((length=in.read(buf))!=-1){
  11. bout.write(buf,0,length);
  12. }
  13. byte[] content = bout.toByteArray();
  14. filecontentEt.setText(new String(content,"UTF-8")); //設置文本框為讀取的內容
  15. in.close();
  16. bout.close();

注意:默認會讀取/data/data/package/files的文件;


二、文件模式介紹


1.Context.MODE_PRIVATE:私有覆蓋模式 - rw- rw- ---

只能被當前應用訪問,並且如果寫入,則覆蓋;

2.Context.MODE_APPEND:私有追加模式 - rw- rw- ---

只能被當前應用訪問,並且如果寫入,則追加;

3.Context,MODE_WORLD_READABLE:公有只讀模式 - rw- rw- r--

可以被其他應用讀取;

4.Context.MODE_WORLD_WRITEABLE:公有可寫模式 - rw- rw- -w-

可以被其他應用寫入,但不能讀取;


注意,如果希望其他使得文件模式疊加,則可以使用加號連接;

比如:Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE 表示其他應用讀寫;


三、簡單應用實例


1.效果圖


目標:當點擊保存時,將以特定的文件名稱和特定的文件內容保存內容,點擊讀取時,將讀取特定的文件的文件內容顯示到文件內容文本框;



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




MainActivity.java

  1. package org.xiazdong.file;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. public class MainActivity extends Activity {
  14. private Button saveButton,readButton;
  15. private EditText filenameEt,filecontentEt;
  16. private Context context = this;
  17. private OnClickListener listener = new OnClickListener(){
  18. @Override
  19. public void onClick(View v) {
  20. if(v==saveButton){
  21. String filename = filenameEt.getText().toString();
  22. String filecontent = filecontentEt.getText().toString();
  23. FileOutputStream out = null;
  24. try {
  25. out = context.openFileOutput(filename, Context.MODE_PRIVATE);
  26. out.write(filecontent.getBytes("UTF-8"));
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. finally{
  31. try {
  32. out.close();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38. else if(v==readButton){
  39. String filename = filenameEt.getText().toString(); //獲得讀取的文件的名稱
  40. FileInputStream in = null;
  41. ByteArrayOutputStream bout = null;
  42. byte[]buf = new byte[1024];
  43. bout = new ByteArrayOutputStream();
  44. int length = 0;
  45. try {
  46. in = context.openFileInput(filename); //獲得輸入流
  47. while((length=in.read(buf))!=-1){
  48. bout.write(buf,0,length);
  49. }
  50. byte[] content = bout.toByteArray();
  51. filecontentEt.setText(new String(content,"UTF-8")); //設置文本框為讀取的內容
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. filecontentEt.invalidate(); //刷新屏幕
  56. try{
  57. in.close();
  58. bout.close();
  59. }
  60. catch(Exception e){}
  61. }
  62. }
  63. };
  64. @Override
  65. public void onCreate(Bundle savedInstanceState) {
  66. super.onCreate(savedInstanceState);
  67. setContentView(R.layout.main);
  68. saveButton = (Button)this.findViewById(R.id.saveButton);
  69. readButton = (Button)this.findViewById(R.id.readButton);
  70. filenameEt = (EditText)this.findViewById(R.id.filename);
  71. filecontentEt = (EditText)this.findViewById(R.id.filecontent);
  72. saveButton.setOnClickListener(listener);
  73. readButton.setOnClickListener(listener);
  74. }
  75. }
Copyright © Linux教程網 All Rights Reserved