使用文件来保存程序中的数据

来源:互联网 发布:辽宁省农产品出口数据 编辑:程序博客网 时间:2024/06/10 05:48

http://blog.csdn.net/zhaokaiqiang1992/article/details/28110317



如何使用文件来保存程序中的数据

标签: 文件系统数据内存手机
 1626人阅读 评论(0) 收藏 举报
 分类:

在程序中,有很多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作


我直接写了一个帮助类,进行文件的写入和读取操作


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 用于在文件中保存程序数据 
  3.  *  
  4.  * @author zhaokaiqiang 
  5.  *  
  6.  */  
  7. public class FileHelper {  
  8.   
  9.     private static final String TAG = "FileHelper";  
  10.     private Context mContext;  
  11.   
  12.     FileHelper(Context _mContext) {  
  13.         mContext = _mContext;  
  14.     }  
  15.   
  16.     // 在手机本地硬盘中保存信息  
  17.     public void save(String fileName, String content) {  
  18.   
  19.         FileOutputStream fileOutputStream = null;  
  20.         try {  
  21.             fileOutputStream = mContext.openFileOutput(fileName,  
  22.                     Context.MODE_PRIVATE);  
  23.             fileOutputStream.write(content.getBytes());  
  24.   
  25.         } catch (FileNotFoundException e) {  
  26.             e.printStackTrace();  
  27.         } catch (IOException e) {  
  28.             e.printStackTrace();  
  29.         } finally {  
  30.             try {  
  31.   
  32.                 if (fileOutputStream != null) {  
  33.                     fileOutputStream.close();  
  34.                 }  
  35.             } catch (IOException e) {  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41.     // 读取手机硬盘中保存的文件  
  42.     public void read(String fileName) {  
  43.         FileInputStream fileInputStream = null;  
  44.         try {  
  45.             fileInputStream = mContext.openFileInput(fileName);  
  46.             int len = 0;  
  47.             byte[] buffer = new byte[1024];  
  48.             ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();  
  49.             while ((len = fileInputStream.read(buffer)) != -1) {  
  50.                 byteArrayInputStream.write(buffer, 0, len);  
  51.             }  
  52.             String string = new String(byteArrayInputStream.toByteArray());  
  53.             Log.d(TAG, string);  
  54.         } catch (FileNotFoundException e) {  
  55.             e.printStackTrace();  
  56.         } catch (IOException e) {  
  57.             e.printStackTrace();  
  58.         } finally {  
  59.             if (fileInputStream != null) {  
  60.                 try {  
  61.                     fileInputStream.close();  
  62.                 } catch (IOException e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             }  
  66.         }  
  67.   
  68.     }  
  69. }  

注意:使用写入操作的时候,写入的内容会将上次写入的内容进行覆盖


写入的文件保存在/data/data/package name/files目录下,使用DDMS可以进行查看

如下图所示:


使用DDMS将文件导出,即可查看内容


上面这些是将数据写入到我们的手机自带的存储空间里,如果想写入我们的SDCard,那么应该怎么做呢?

下面的写入到SDCard的操作


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // save infomation in the SDCard  
  2.     public boolean saveToSDCard(String fileName, String content) {  
  3.   
  4.         // judge weather the SDCard exits,and can be read and written  
  5.         if (!Environment.getExternalStorageState().equals(  
  6.                 Environment.MEDIA_MOUNTED)) {  
  7.             return false;  
  8.         }  
  9.   
  10.         FileOutputStream fileOutputStream = null;  
  11.         File file = new File(Environment.getExternalStorageDirectory(),  
  12.                 fileName);  
  13.         try {  
  14.             fileOutputStream = new FileOutputStream(file);  
  15.             fileOutputStream.write(content.getBytes());  
  16.             return true;  
  17.         } catch (FileNotFoundException e) {  
  18.             e.printStackTrace();  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.         } finally {  
  22.             try {  
  23.   
  24.                 if (fileOutputStream != null) {  
  25.                     fileOutputStream.close();  
  26.                 }  
  27.             } catch (IOException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.         return false;  
  32.     }  

下面是读取位于SDCard根目录下文件的操作方法

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // read the file in the SDCard  
  2.     public String readFromSD(String fileName) {  
  3.         FileInputStream fileInputStream = null;  
  4.         File file = new File(Environment.getExternalStorageDirectory(),  
  5.                 fileName);  
  6.         try {  
  7.             fileInputStream = new FileInputStream(file);  
  8.             int len = 0;  
  9.             byte[] buffer = new byte[1024];  
  10.             ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();  
  11.             while ((len = fileInputStream.read(buffer)) != -1) {  
  12.                 byteArrayInputStream.write(buffer, 0, len);  
  13.             }  
  14.             String string = new String(byteArrayInputStream.toByteArray());  
  15.             return string;  
  16.         } catch (FileNotFoundException e) {  
  17.             e.printStackTrace();  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         } finally {  
  21.             if (fileInputStream != null) {  
  22.                 try {  
  23.                     fileInputStream.close();  
  24.                 } catch (IOException e) {  
  25.                     e.printStackTrace();  
  26.                 }  
  27.             }  
  28.         }  
  29.   
  30.         return null;  
  31.     }  


0 0
原创粉丝点击