Android中的文件存储数据方式 .

 

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);

 

下面通过程序来演示下文件存储的使用。完整代码下载:android_files.rar

  1. /** 
  2.  * MainActivity 
  3.  *  
  4.  * @author zuolongsnail 
  5.  *  
  6.  */  
  7. public class MainActivity extends Activity {  
  8.     private EditText writeET;  
  9.     private Button writeBtn;  
  10.     private TextView contentView;  
  11.     public static final String FILENAME = "setting.set";  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         writeET = (EditText) findViewById(R.id.write_et);  
  18.         writeBtn = (Button) findViewById(R.id.write_btn);  
  19.         contentView = (TextView) findViewById(R.id.contentview);  
  20.         writeBtn.setOnClickListener(new OperateOnClickListener());  
  21.     }  
  22.   
  23.     class OperateOnClickListener implements OnClickListener {  
  24.         @Override  
  25.         public void onClick(View v) {  
  26.             writeFiles(writeET.getText().toString());  
  27.             contentView.setText(readFiles());  
  28.             System.out.println(getFilesDir());  
  29.         }  
  30.     }  
  31.   
  32.     // 保存文件内容   
  33.     private void writeFiles(String content) {  
  34.         try {  
  35.             // 打开文件获取输出流,文件不存在则自动创建   
  36.             FileOutputStream fos = openFileOutput(FILENAME,  
  37.                     Context.MODE_PRIVATE);  
  38.             fos.write(content.getBytes());  
  39.             fos.close();  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.     }  
  44.   
  45.     // 读取文件内容   
  46.     private String readFiles() {  
  47.         String content = null;  
  48.         try {  
  49.             FileInputStream fis = openFileInput(FILENAME);  
  50.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  51.             byte[] buffer = new byte[1024];  
  52.             int len = 0;  
  53.             while ((len = fis.read(buffer)) != -1) {  
  54.                 baos.write(buffer, 0, len);  
  55.             }  
  56.             content = baos.toString();  
  57.             fis.close();  
  58.             baos.close();  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         }  
  62.         return content;  
  63.     }  
  64. }  

程序截图:

 

提供一个文件存储数据的工具类:

  1. /** 
  2.  * 文件存储数据方式工具类 
  3.  *  
  4.  * @author zuolongsnail 
  5.  */  
  6. public class FilesUtil {  
  7.     /** 
  8.      * 保存文件内容 
  9.      *  
  10.      * @param c 
  11.      * @param fileName 
  12.      *            文件名称 
  13.      * @param content 
  14.      *            内容 
  15.      */  
  16.     private void writeFiles(Context c, String fileName, String content, int mode)  
  17.             throws Exception {  
  18.         // 打开文件获取输出流,文件不存在则自动创建   
  19.         FileOutputStream fos = c.openFileOutput(fileName, mode);  
  20.         fos.write(content.getBytes());  
  21.         fos.close();  
  22.     }  
  23.   
  24.     /** 
  25.      * 读取文件内容 
  26.      *  
  27.      * @param c 
  28.      * @param fileName 
  29.      *            文件名称 
  30.      * @return 返回文件内容 
  31.      */  
  32.     private String readFiles(Context c, String fileName) throws Exception {  
  33.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  34.         FileInputStream fis = c.openFileInput(fileName);  
  35.         byte[] buffer = new byte[1024];  
  36.         int len = 0;  
  37.         while ((len = fis.read(buffer)) != -1) {  
  38.             baos.write(buffer, 0, len);  
  39.         }  
  40.         String content = baos.toString();  
  41.         fis.close();  
  42.         baos.close();  
  43.         return content;  
  44.     }  
  45. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值