你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2012-07-22] 来源:Linux社区  作者:xiazdong [字体:大 中 小]

Android入门:File文件存储 教程链接:http://www.linuxidc.com/Linux/2012-07/66002.htm

  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内容  
linux
相关资讯       Android入门教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款