getCacheDir用法

注: 在Activity中有 getFileDir() 和 getCacheDir(); 方法可以获得当前的手机自带的存储空间中的当前包文件的路径
getFileDir() ----- /data/data/cn.xxx.xxx(当前包)/files
getCacheDir() ----- /data/data/cn.xxx.xxx(当前包)/cache

 
  1.   
  2. 1. 编写文件读取与写入功能实现类 FileService   
  3.   
  4.     package cn.android.service;   
  5.   
  6.     import java.io.ByteArrayOutputStream;   
  7.     import java.io.FileInputStream;   
  8.     import java.io.FileOutputStream;   
  9.   
  10.     import android.content.Context;   
  11.     import android.util.Log;   
  12.   
  13.     /**  
  14.      * 文件保存与读取功能实现类  
  15.      * @author Administrator  
  16.      *  
  17.      * 2010-6-28 下午08:15:18  
  18.      */  
  19.     public class FileService {   
  20.   
  21.         public static final String TAG = "FileService";   
  22.         private Context context;   
  23.   
  24.         //得到传入的上下文对象的引用   
  25.         public FileService(Context context) {   
  26.             this.context = context;   
  27.         }   
  28.   
  29.         /**  
  30.          * 保存文件  
  31.          *   
  32.          * @param fileName 文件名  
  33.          * @param content  文件内容  
  34.          * @throws Exception  
  35.          */  
  36.         public void save(String fileName, String content) throws Exception {   
  37.   
  38.             // 由于页面输入的都是文本信息,所以当文件名不是以.txt后缀名结尾时,自动加上.txt后缀   
  39.             if (!fileName.endsWith(".txt")) {   
  40.                 fileName = fileName + ".txt";   
  41.             }   
  42.                
  43.             byte[] buf = fileName.getBytes("iso8859-1");   
  44.                
  45.             Log.e(TAG, new String(buf,"utf-8"));   
  46.                
  47.             fileName = new String(buf,"utf-8");   
  48.                
  49.             Log.e(TAG, fileName);   
  50.                
  51.             // Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND   
  52.             // Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。   
  53.             // Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。   
  54.             // MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。   
  55.             // 如果希望文件被其他应用读和写,可以传入:   
  56.             // openFileOutput("output.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);   
  57.   
  58.             FileOutputStream fos = context.openFileOutput(fileName, context.MODE_PRIVATE);   
  59.             fos.write(content.getBytes());   
  60.             fos.close();   
  61.         }   
  62.   
  63.         /**  
  64.          * 读取文件内容  
  65.          *   
  66.          * @param fileName 文件名  
  67.          * @return 文件内容  
  68.          * @throws Exception  
  69.          */  
  70.         public String read(String fileName) throws Exception {   
  71.   
  72.             // 由于页面输入的都是文本信息,所以当文件名不是以.txt后缀名结尾时,自动加上.txt后缀   
  73.             if (!fileName.endsWith(".txt")) {   
  74.                 fileName = fileName + ".txt";   
  75.             }   
  76.   
  77.             FileInputStream fis = context.openFileInput(fileName);   
  78.             ByteArrayOutputStream baos = new ByteArrayOutputStream();   
  79.   
  80.             byte[] buf = new byte[1024];   
  81.             int len = 0;   
  82.   
  83.             //将读取后的数据放置在内存中---ByteArrayOutputStream   
  84.             while ((len = fis.read(buf)) != -1) {   
  85.                 baos.write(buf, 0, len);   
  86.             }   
  87.   
  88.             fis.close();   
  89.             baos.close();   
  90.   
  91.             //返回内存中存储的数据   
  92.             return baos.toString();   
  93.   
  94.         }   
  95.   
  96.     }   
  97.   
  98. 2. 编写Activity类:   
  99.     package cn.android.test;   
  100.   
  101.     import android.app.Activity;   
  102.     import android.os.Bundle;   
  103.     import android.util.Log;   
  104.     import android.view.View;   
  105.     import android.widget.Button;   
  106.     import android.widget.EditText;   
  107.     import android.widget.Toast;   
  108.     import cn.android.service.FileService;   
  109.   
  110.     public class TestAndroidActivity extends Activity {   
  111.         /** Called when the activity is first created. */  
  112.            
  113.         //得到FileService对象   
  114.         private FileService fileService = new FileService(this);   
  115.         //定义视图中的filename输入框对象   
  116.         private EditText fileNameText;   
  117.         //定义视图中的contentText输入框对象   
  118.         private EditText contentText;   
  119.         //定义一个土司提示对象   
  120.         private Toast toast;   
  121.   
  122.            
  123.         @Override  
  124.         public void onCreate(Bundle savedInstanceState) {   
  125.         super.onCreate(savedInstanceState);   
  126.         setContentView(R.layout.main);   
  127.              
  128.         //得到视图中的两个输入框和两个按钮的对象引用   
  129.         Button button = (Button)this.findViewById(R.id.button);   
  130.         Button read = (Button)this.findViewById(R.id.read);   
  131.         fileNameText = (EditText) this.findViewById(R.id.filename);   
  132.         contentText = (EditText) this.findViewById(R.id.content);   
  133.            
  134.         //为保存按钮添加保存事件   
  135.         button.setOnClickListener(new View.OnClickListener() {   
  136.                 @Override  
  137.                 public void onClick(View v) {   
  138.                        
  139.                     String fileName = fileNameText.getText().toString();   
  140.                     String content = contentText.getText().toString();   
  141.                        
  142.                     //当文件名为空的时候,提示用户文件名为空,并记录日志。   
  143.                     if(isEmpty(fileName)) {   
  144.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.empty_filename, Toast.LENGTH_LONG);   
  145.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  146.                         toast.show();      
  147.                         Log.w(fileService.TAG, "The file name is empty");   
  148.                         return;   
  149.                     }   
  150.                        
  151.                     //当文件内容为空的时候,提示用户文件内容为空,并记录日志。   
  152.                     if(isEmpty(content)) {   
  153.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.empty_content, Toast.LENGTH_LONG);   
  154.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  155.                         toast.show();      
  156.                         Log.w(fileService.TAG, "The file content is empty");   
  157.                         return;   
  158.                     }   
  159.                        
  160.                     //当文件名和内容都不为空的时候,调用fileService的save方法   
  161.                     //当成功执行的时候,提示用户保存成功,并记录日志   
  162.                     //当出现异常的时候,提示用户保存失败,并记录日志   
  163.                     try {   
  164.                         fileService.save(fileName, content);   
  165.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.success, Toast.LENGTH_LONG);   
  166.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  167.                         toast.show();      
  168.                         Log.i(fileService.TAG, "The file save successful");   
  169.                     } catch (Exception e) {   
  170.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.fail, Toast.LENGTH_LONG);   
  171.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  172.                         toast.show();      
  173.                         Log.e(fileService.TAG, "The file save failed");   
  174.                     }   
  175.                        
  176.                 }   
  177.         });   
  178.            
  179.            
  180.         //为读取按钮添加读取事件   
  181.         read.setOnClickListener(new View.OnClickListener() {   
  182.                 @Override  
  183.                 public void onClick(View v) {   
  184.                        
  185.                     //得到文件名输入框中的值   
  186.                     String fileName = fileNameText.getText().toString();   
  187.                        
  188.                     //如果文件名为空,则提示用户输入文件名,并记录日志   
  189.                     if(isEmpty(fileName)) {   
  190.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.empty_filename, Toast.LENGTH_LONG);   
  191.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  192.                         toast.show();      
  193.                         Log.w(fileService.TAG, "The file name is empty");   
  194.                         return;   
  195.                     }   
  196.                        
  197.                     //调用fileService的read方法,并将读取出来的内容放入到文本内容输入框里面   
  198.                     //如果成功执行,提示用户读取成功,并记录日志。   
  199.                     //如果出现异常信息(例:文件不存在),提示用户读取失败,并记录日志。   
  200.                     try {   
  201.                         contentText.setText(fileService.read(fileName));   
  202.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.read_success, Toast.LENGTH_LONG);   
  203.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  204.                         toast.show();      
  205.                         Log.i(fileService.TAG, "The file read successful");   
  206.                     } catch (Exception e) {   
  207.                         toast = Toast.makeText(TestAndroidActivity.this, R.string.read_fail, Toast.LENGTH_LONG);   
  208.                         toast.setMargin(RESULT_CANCELED, 0.345f);                                      
  209.                         toast.show();      
  210.                         Log.e(fileService.TAG, "The file read failed");   
  211.                     }   
  212.                 }   
  213.         });   
  214.            
  215.            
  216.         }   
  217.            
  218.         //编写一个isEmpty方法,判断字符串是否为空   
  219.         private boolean isEmpty(String s) {   
  220.         if(s == null || "".equals(s.trim())) {   
  221.             return true;   
  222.         }   
  223.         return false;   
  224.         }   
  225.            
  226.     } 

posted on 2012-04-21 18:25  jiezzy  阅读(7839)  评论(1编辑  收藏  举报