织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

Android文件读写实例代码

jackyhwei 发布于 2011-10-26 13:05 点击:次 
为了能对sdcard进行读写操作,即可创建文件或目录,需要在AndroidManifest.xml中添加权限的声明
TAG:

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;   
  51.     }  


   

(rnmichelle)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201110/15209.html]
本文出处:rnmichelle.blog.51cto.com 作者:rnmichelle
顶一下
(2)
50%
踩一下
(2)
50%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容