android 保存和读取文件

来源:互联网 发布:推荐好的淘宝古风店铺 编辑:程序博客网 时间:2024/04/30 02:32
  1. package com.pdsu.file;  
  2.   
  3. import java.io.BufferedWriter;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.OutputStreamWriter;  
  10.   
  11. import android.content.Context;  
  12. import android.os.Environment;  
  13.   
  14. /** 
  15.  * 文件服务类 
  16.  *  
  17.  * @author huangyinge 
  18.  *  
  19.  */  
  20. public class FileService {  
  21.     private Context context;  
  22.   
  23.     public FileService(Context context) {  
  24.         super();  
  25.         this.context = context;  
  26.     }  
  27.   
  28.     /** 
  29.      * 文件保存 
  30.      *  
  31.      * @param fileName 
  32.      *            文件名称 
  33.      * @param fileContent 
  34.      *            文件内容 
  35.      *             
  36.      *            Context.MODE_PRIVATE 
  37.      *              Private方式创建的文件只能被本应用访问,每次都会创建一个新的文件,如果文件存在就覆盖原来的文件 
  38.      *            Context.MODE_APPEND 
  39.      *               APPEND 方式创建的文件,文件也只能被本应用访问,如果文件存在就在文件的末尾添加内容,如果不存在就创建一个新的文件 
  40.      *            Context.MODE_WORLD_READABLE 
  41.      *                创建的文件其他应用程序可以读取该文件  ,但不能写入 
  42.      *            Context.MODE_WORLD_WRITEABLE 
  43.      *              创建的文件可以被其他应用程序写入数据,但不能读取数据 
  44.      *             
  45.      *            Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE 
  46.      *              将会允许其他应用程序读取和写入数据 
  47.      */  
  48.     public boolean savePrivate(String fileName, String fileContent) {  
  49.         return save(fileName,fileContent,Context.MODE_PRIVATE);  
  50.     }  
  51.     //追加内容  
  52.     public boolean saveAppend(String fileName, String fileContent) {  
  53.         return save(fileName,fileContent,Context.MODE_APPEND);  
  54.     }  
  55.     //可写入  
  56.     public boolean saveWriteable(String fileName, String fileContent) {  
  57.         return save(fileName,fileContent,Context.MODE_WORLD_WRITEABLE);  
  58.     }  
  59.     //可读取  
  60.     public boolean saveReadable(String fileName, String fileContent) {  
  61.         return save(fileName,fileContent,Context.MODE_WORLD_READABLE);  
  62.     }  
  63.     //保存文件到sdcard  
  64.     public boolean saveToSdcard(String fileName, String fileContent) {  
  65.         boolean flag = false;  
  66.         try {   
  67.             FileOutputStream fos = new FileOutputStream(  
  68.                     Environment.getExternalStorageDirectory()+File.separator+fileName,true);  
  69.             flag = write(fileContent, fos);  
  70.         } catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         }  
  73.         return flag;  
  74.     }  
  75.     /** 
  76.      * 保存文件 
  77.      * @param fileName 指定文件名 
  78.      * @param fileContent 文件内容 
  79.      * @param mode 文件模式 
  80.      * @return 
  81.      */  
  82.     public boolean save(String fileName, String fileContent,int mode) {  
  83.         boolean flag = false;  
  84.         try {  
  85.             FileOutputStream fos = context.openFileOutput(fileName,  
  86.                     mode);  
  87.             flag = write(fileContent, fos);  
  88.         } catch (Exception e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.         return flag;  
  92.     }  
  93.     private boolean write(String fileContent, FileOutputStream fos)  
  94.             throws IOException {  
  95.         boolean flag;  
  96.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));  
  97.         bw.write(fileContent);  
  98.         bw.flush();  
  99.         bw.close();  
  100.         flag = true;  
  101.         return flag;  
  102.     }  
  103.     /** 
  104.      * 读取文本内容 
  105.      * @param fileName 文件名称 
  106.      * @return  
  107.      */  
  108.     public String read(String fileName){  
  109.         String content = "";  
  110.         try {  
  111.             FileInputStream fis = context.openFileInput(fileName);  
  112.             ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  113.             int len  = 0;  
  114.             byte buf[] = new byte[1024];  
  115.             while((len = fis.read(buf))!=-1){  
  116.                 bos.write(buf, 0, len);  
  117.             }  
  118.             fis.close();  
  119.             content = bos.toString();  
  120.             bos.close();  
  121.         } catch (Exception e) {  
  122.             e.printStackTrace();  
  123.         }  
  124.         return content;  
  125.     }  
  126. }  
0 0
原创粉丝点击