Android入门:File文件存储


数据的存储有多种方式,比如数据库存储、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.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7.   
  8. import android.app.Activity;  
  9. import android.content.Context;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15.   
  16. public class MainActivity extends Activity {  
  17.     private Button saveButton,readButton;  
  18.     private EditText filenameEt,filecontentEt;  
  19.     private Context context = this;  
  20.     private OnClickListener listener = new OnClickListener(){  
  21.         @Override  
  22.         public void onClick(View v) {  
  23.             if(v==saveButton){  
  24.                 String filename = filenameEt.getText().toString();  
  25.                 String filecontent = filecontentEt.getText().toString();  
  26.                 FileOutputStream out = null;  
  27.                 try {  
  28.                     out = context.openFileOutput(filename, Context.MODE_PRIVATE);  
  29.                     out.write(filecontent.getBytes("UTF-8"));  
  30.                 } catch (Exception e) {  
  31.                     e.printStackTrace();  
  32.                 }  
  33.                 finally{  
  34.                     try {  
  35.                         out.close();  
  36.                     } catch (Exception e) {  
  37.                         e.printStackTrace();  
  38.                     }  
  39.                 }  
  40.             }  
  41.             else if(v==readButton){  
  42.                 String filename = filenameEt.getText().toString(); //获得读取的文件的名称   
  43.                 FileInputStream in = null;  
  44.                 ByteArrayOutputStream bout = null;  
  45.                 byte[]buf = new byte[1024];  
  46.                 bout = new ByteArrayOutputStream();  
  47.                 int length = 0;  
  48.                 try {  
  49.                     in = context.openFileInput(filename); //获得输入流   
  50.                     while((length=in.read(buf))!=-1){  
  51.                         bout.write(buf,0,length);  
  52.                     }  
  53.                     byte[] content = bout.toByteArray();  
  54.                     filecontentEt.setText(new String(content,"UTF-8")); //设置文本框为读取的内容   
  55.                 } catch (Exception e) {  
  56.                     e.printStackTrace();  
  57.                 }  
  58.                 filecontentEt.invalidate(); //刷新屏幕   
  59.                 try{  
  60.                     in.close();  
  61.                     bout.close();  
  62.                 }  
  63.                 catch(Exception e){}  
  64.             }  
  65.         }  
  66.           
  67.     };  
  68.     @Override  
  69.     public void onCreate(Bundle savedInstanceState) {  
  70.         super.onCreate(savedInstanceState);  
  71.         setContentView(R.layout.main);  
  72.         saveButton = (Button)this.findViewById(R.id.saveButton);  
  73.         readButton = (Button)this.findViewById(R.id.readButton);  
  74.         filenameEt = (EditText)this.findViewById(R.id.filename);  
  75.         filecontentEt = (EditText)this.findViewById(R.id.filecontent);  
  76.         saveButton.setOnClickListener(listener);  
  77.         readButton.setOnClickListener(listener);  
  78.     }  
  • 1
  • 2
  • 下一页

相关内容