Android文件读写实例代码

转载:http://rnmichelle.javaeye.com/blog/923217

本文转载自本人在javaeye.com 上发表的原创博客,有兴趣的朋友可以看看。

Android文件读写实例代码

1.Manifest文件中权限的声明
为了能对sdcard进行读写操作,即可创建文件或目录,需要在AndroidManifest.xml中添加权限的声明:

 
 
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   

2.创建目录及目标文件,并以"utf-8"编码格式对文件写入
首先可在目标文件开头定义一下目录及文件名常量,方便创建文件时用

 
 
  1. final static String FOLDER = "/sample/";   
  2. final static String FILENAME = "sample";    
  3. final static String SUFFIX = ".txt"// suffix could be replaced on demand   

writeFile函数按命名创建目录和文件并写入字串

 
 
  1. private void writeFile(StringBuilder sb) {
  2. String foldername = Environment.getExternalStorageDirectory().getPath()   
  3.                              + FOLDER;   
  4.     File folder = new File(foldername);   
  5.     if (folder != null && !folder.exists()) {   
  6.         if (!folder.mkdir() && !folder.isDirectory())   
  7.         {   
  8.             Log.d(TAG, "Error: make dir failed!");   
  9.             return;   
  10.         }   
  11.     }   
  12.     
  13.     String stringToWrite = sb.toString();   
  14.     String targetPath = foldername + FILENAME + SUFFIX;   
  15.     File targetFile = new File(targetPath);   
  16.     if (targetFile != null) {   
  17.         if (targetFile.exists()) {   
  18.             targetFile.delete();   
  19.         }   
  20.     
  21.         OutputStreamWriter osw;   
  22.         try{   
  23.             osw = new OutputStreamWriter(   
  24.                         new FileOutputStream(targetFile),"utf-8");   
  25.             try {   
  26.             osw.write(stringToWrite);   
  27.                 osw.flush();   
  28.                 osw.close();   
  29.             } catch (IOException e) {   
  30.                 // TODO Auto-generated catch block   
  31.                 e.printStackTrace();   
  32.             }   
  33.         } catch (UnsupportedEncodingException e1) {   
  34.             // TODO Auto-generated catch block   
  35.             e1.printStackTrace();   
  36.         } catch (FileNotFoundException e1) {   
  37.             // TODO Auto-generated catch block   
  38.             e1.printStackTrace();   
  39.         }   
  40. }   
  41. }

注意在new FileOutputStream时用到编码方式为"utf-8",即以"utf-8"格式来保存这个,如果想用别的格式来保存,可换成"GB2312","Big5"等。
此外,待写入的字串可以将字串样式先用StringBuilder构造好,直接用StringBuilder变量;也可以直接传入String类型的变量。

3.以"utf-8"解码格式读入文

 
 
  1. private String readFile(String filepath) {   
  2.     String path = filepath;   
  3.     if (null == path) {   
  4.         Log.d(TAG, "Error: Invalid file name!");   
  5.         return null;   
  6.     }   
  7.  
  8.     String filecontent = null;   
  9.     File f = new File(path);   
  10.     if (f != null && f.exists())   
  11.     {
  12.         FileInputStream fis = null;   
  13.         try {   
  14.             fis = new FileInputStream(f);   
  15.         } catch (FileNotFoundException e1) {   
  16.             // TODO Auto-generated catch block   
  17.             e1.printStackTrace();   
  18.             Log.d(TAG, "Error: Input File not find!");   
  19.             return null;   
  20.         }   
  21.   
  22.         CharBuffer cb;   
  23.         try {   
  24.             cb = CharBuffer.allocate(fis.available());   
  25.         } catch (IOException e1) {   
  26.             // TODO Auto-generated catch block   
  27.             e1.printStackTrace();   
  28.             Log.d(TAG, "Error: CharBuffer initial failed!");   
  29.             return null;   
  30.         }   
  31.     
  32.         InputStreamReader isr;   
  33.         try {   
  34.             isr = new InputStreamReader(fis, "utf-8");   
  35.             try {   
  36.                 if (cb != null) {   
  37.                    isr.read(cb);   
  38.                 }   
  39.                 filecontent = new String(cb.array());   
  40.                 isr.close();   
  41.             } catch (IOException e) {   
  42.                 e.printStackTrace();   
  43.             }   
  44.         } catch (UnsupportedEncodingException e) {   
  45.             // TODO Auto-generated catch block   
  46.             e.printStackTrace();           
  47.         }   
  48.     }   
  49.     Log.d(TAG, "readFile filecontent = " + filecontent);   
  50.     return filecontent;   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值