Android文件读写实例代码
2012-07-27 10:35
531 查看
Android文件读写实例代码
本文主要是基于Android的文件创建和读写代码的例子,作为本人的代码资源记录下来,方便以后开发直接利用。现以在sdcard/sample目录创建名称为sample的txt文件的读写操作为例,说明代码实现过程。
1.Manifest文件中权限的声明
为了能对sdcard进行读写操作,即可创建文件或目录,需要在AndroidManifest.xml中添加权限的声明:
2.创建目录及目标文件,并以"utf-8"编码格式对文件写入
首先可在目标文件开头定义一下目录及文件名常量,方便创建文件时用
writeFile函数按命名创建目录和文件并写入字串
注意在new FileOutputStream时用到编码方式为"utf-8",即以"utf-8"格式来保存这个,如果想用别的格式来保存,可换成"GB2312","Big5"等。
此外,待写入的字串可以将字串样式先用StringBuilder构造好,直接用StringBuilder变量;也可以直接传入String类型的变量。
3.以"utf-8"解码格式读入文件
本文主要是基于Android的文件创建和读写代码的例子,作为本人的代码资源记录下来,方便以后开发直接利用。现以在sdcard/sample目录创建名称为sample的txt文件的读写操作为例,说明代码实现过程。
1.Manifest文件中权限的声明
为了能对sdcard进行读写操作,即可创建文件或目录,需要在AndroidManifest.xml中添加权限的声明:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.创建目录及目标文件,并以"utf-8"编码格式对文件写入
首先可在目标文件开头定义一下目录及文件名常量,方便创建文件时用
final static String FOLDER = "/sample/"; final static String FILENAME = "sample"; final static String SUFFIX = ".txt"; // suffix could be replaced on demand
writeFile函数按命名创建目录和文件并写入字串
private void writeFile(StringBuilder sb) { String foldername = Environment.getExternalStorageDirectory().getPath() + FOLDER; File folder = new File(foldername); if (folder != null && !folder.exists()) { if (!folder.mkdir() && !folder.isDirectory()) { Log.d(TAG, "Error: make dir failed!"); return; } } String stringToWrite = sb.toString(); String targetPath = foldername + FILENAME + SUFFIX; File targetFile = new File(targetPath); if (targetFile != null) { if (targetFile.exists()) { targetFile.delete(); } OutputStreamWriter osw; try{ osw = new OutputStreamWriter( new FileOutputStream(targetFile),"utf-8"); try { osw.write(stringToWrite); osw.flush(); osw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
注意在new FileOutputStream时用到编码方式为"utf-8",即以"utf-8"格式来保存这个,如果想用别的格式来保存,可换成"GB2312","Big5"等。
此外,待写入的字串可以将字串样式先用StringBuilder构造好,直接用StringBuilder变量;也可以直接传入String类型的变量。
3.以"utf-8"解码格式读入文件
private String readFile(String filepath) { String path = filepath; if (null == path) { Log.d(TAG, "Error: Invalid file name!"); return null; } String filecontent = null; File f = new File(path); if (f != null && f.exists()) { FileInputStream fis = null; try { fis = new FileInputStream(f); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); Log.d(TAG, "Error: Input File not find!"); return null; } CharBuffer cb; try { cb = CharBuffer.allocate(fis.available()); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); Log.d(TAG, "Error: CharBuffer initial failed!"); return null; } InputStreamReader isr; try { isr = new InputStreamReader(fis, "utf-8"); try { if (cb != null) { isr.read(cb); } filecontent = new String(cb.array()); isr.close(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Log.d(TAG, "readFile filecontent = " + filecontent); return filecontent; }
相关文章推荐
- Android文件读写实例代码
- Android文件读写实例代码
- Android文件读写实例代码
- Android中的webview支持页面中的文件上传实例代码
- android使用AES加密和解密文件实例代码
- Android中发送Http请求(包括文件上传、servlet接收)的实例代码
- Android Uri和文件路径互相转换的实例代码
- Android:java byte数组文件读写学习实例
- js读写json文件实例代码
- android开发之Json文件的读写的示例代码
- Android 实例子源代码文件下载地址380个合集
- android读写文件实例
- android 上传文件到服务器代码实例
- android中把文件保存到sdcard代码实例
- Android 通过httppost上传文本文件到服务器的实例代码
- android实现蓝牙文件发送的实例代码,支持多种机型
- android 上传文件到服务器代码实例
- js读写json文件实例代码
- android中把文件保存到sdcard代码实例
- android怎么在代码里获得系统文件的读写权限?