Chinaunix首页 | 论坛 | 博客
  • 博客访问: 460553
  • 博文数量: 155
  • 博客积分: 2954
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-12 22:00
文章分类

全部博文(155)

文章存档

2014年(2)

2013年(5)

2012年(10)

2011年(33)

2010年(105)

我的朋友

分类: 嵌入式

2010-10-21 10:27:41

Java代码 复制代码
  1. package tl.android.utils;   
  2.   
  3. import java.io.FileInputStream;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7. import java.io.InputStream;   
  8. import android.content.Context;   
  9. import android.util.Log;   
  10.   
  11. /**  
  12.  *   
  13.  * @author amos_tl  
  14.  * @version 1.0  
  15.  *   
  16.  * 实现Android应用程序私有文件或静态资源文件的读取与写入功能.  
  17.  *   
  18.  */  
  19.   
  20. public class FileUtils extends Object{   
  21.     /**  
  22.      * LogCat TAG 标识  
  23.      */  
  24.     public static final String TAG = "FileUtils";   
  25.        
  26.     /**  
  27.      * 读取应用程序私有文件.相对路径: /data/data/应用程序包/files/  
  28.      * @param fileName 想要读取的文件名  
  29.      * @param buffer 字节方式读取到缓存  
  30.      * @param context 关联的应用程序上下文  
  31.      */  
  32.     public static void readFile(String fileName, byte[] buffer, Context context){   
  33.         FileInputStream fis = null;   
  34.            
  35.         try {   
  36.             fis = context.openFileInput(fileName);   
  37.             try {   
  38.                 fis.read(buffer);   
  39.             } catch (IOException e) {   
  40.                 Log.e(TAG, e.getMessage(), e);   
  41.             } finally{   
  42.                 Log.d(TAG, buffer.length + "bytes ");   
  43.             }   
  44.         } catch (FileNotFoundException e) {   
  45.             Log.e(TAG, e.getMessage(), e);   
  46.         }   
  47.            
  48.     }   
  49.        
  50.     /**  
  51.      * 写入应用程序私有文件.相对路径: /data/data/应用程序包/files/  
  52.      * @param fileName 想要写入的文件名,没有则创建  
  53.      * @param mode 写入模式  
  54.      * @param buffer 要写入的字节数组  
  55.      * @param context 关联的应用程序上下文  
  56.      */  
  57.     public static void writeFile(String fileName, int mode, byte[] buffer, Context context){   
  58.         FileOutputStream fos = null;    
  59.         try {   
  60.             fos = context.openFileOutput(fileName, mode);   
  61.             try {   
  62.                 fos.write(buffer);   
  63.                 fos.flush();   
  64.             } catch (IOException e) {   
  65.                 Log.e(TAG, e.getMessage(), e);   
  66.             }finally{   
  67.                 Log.d(TAG, buffer.length + "bytes");   
  68.             }   
  69.         } catch (FileNotFoundException e) {   
  70.             Log.e(TAG, e.getMessage(), e);   
  71.         }   
  72.     }   
  73.        
  74.     /**  
  75.      * 读取应用程序静态资源文件  
  76.      * @param id 资源文件ID  
  77.      * @param buffer 字节方式读取到缓存  
  78.      * @param context 关联的应用程序上下文  
  79.      */  
  80.     public static void readRawFile(int id, byte[] buffer, Context context){   
  81.         InputStream is = null;   
  82.         try{   
  83.             is = context.getResources().openRawResource(id);   
  84.             is.read(buffer);   
  85.         }catch(Exception e){   
  86.             Log.e(TAG, e.getMessage(), e);   
  87.         }finally{   
  88.             Log.d(TAG, buffer.length + "bytes ");   
  89.         }   
  90.     }   
  91.        
  92.        
  93.        
  94. }  
阅读(879) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~