Android入门:实现一个File存储的辅助类


Android入门:File文件存储 教程链接:

  1. package com.xiazdong.file.util;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9.   
  10. import android.content.Context;  
  11. import android.os.Environment;  
  12.   
  13. public class FileUtil {  
  14.     /** 
  15.      * 保存文本到内存 
  16.      * @param context 
  17.      * @param filename 
  18.      * @param content 
  19.      * @param mode 
  20.      * @throws Exception 
  21.      */  
  22.     public static void saveTextInMemory(Context context,String filename,String content,int mode) throws Exception{  
  23.         try{  
  24.             FileOutputStream out = context.openFileOutput(filename, mode);  
  25.             out.write(content.getBytes("UTF-8"));  
  26.             out.close();  
  27.         }  
  28.         catch(Exception e){  
  29.             throw new Exception();  
  30.         }  
  31.     }  
  32.     /** 
  33.      * 保存文件到sdcard 
  34.      * @param filename 
  35.      * @param content 
  36.      * @throws Exception 
  37.      */  
  38.     public static void saveTextInSdcard(String filename,String content) throws Exception{  
  39.         try{  
  40.             File f = new File(Environment.getExternalStorageDirectory(),filename);  
  41.             FileOutputStream out = new FileOutputStream(f);  
  42.             out.write(content.getBytes("UTF-8"));  
  43.             out.close();  
  44.         }  
  45.         catch(Exception e){  
  46.             throw new Exception();  
  47.         }  
  48.     }  
  49.     /** 
  50.      * 从内存读取文件 
  51.      * @param filename 
  52.      * @return 
  53.      * @throws Exception 
  54.      */  
  55.     public static String loadTextFromSdcard(String filename) throws Exception{  
  56.         try{  
  57.             File f = new File(Environment.getExternalStorageDirectory(),filename);  
  58.             FileInputStream in = new FileInputStream(f);  
  59.             byte[]data = read2byte(in);  
  60.             return new String(data,"UTF-8");  
  61.         }  
  62.         catch(Exception e){  
  63.             throw new Exception();  
  64.         }  
  65.     }  
  66.     /** 
  67.      * 从sdcard读取文件  
  68.      * @param context 
  69.      * @param filename 
  70.      * @return 
  71.      * @throws Exception 
  72.      */  
  73.     public static String loadTextFromMemory(Context context,String filename) throws Exception{  
  74.         try{  
  75.             FileInputStream in = context.openFileInput(filename);  
  76.             byte[]data = read2byte(in);  
  77.             return new String(data,"UTF-8");  
  78.         }  
  79.         catch(Exception e){  
  80.             throw new Exception();  
  81.         }  
  82.     }  
  83.     private static byte[] read2byte(InputStream in) throws IOException {  
  84.         byte[] data;  
  85.         ByteArrayOutputStream bout = new ByteArrayOutputStream();  
  86.         byte[]buf = new byte[1024];  
  87.         int len = 0;  
  88.         while((len = in.read(buf))!=-1){  
  89.             bout.write(buf, 0, len);  
  90.         }  
  91.         data = bout.toByteArray();  
  92.         return data;  
  93.     }  
  94. }  

测试代码:

  1. FileUtil.saveTextInSdcard("1.txt","hello");     //将"hello"保存到/mnt/sdcard/1.txt中   
  2. String content = FileUtil.loadTextFromSdcard("1.txt");  //读取/mnt/sdcard/1.txt内容   
  3. FileUtil.saveTextInMemory(MainActivity.this,"1.txt","hello", Context.MODE_PRIVATE); //将hello保存到/data/data/package/files/1.txt中   
  4. String content = FileUtil.loadTextFromMemory(MainActivity.this"1.txt");   //读取/data/data/package/files/1.txt内容  

相关内容