Android实现读写SD卡
内容摘要
SD卡的读写是我们在开发Android 应用程序过程中最常见的操作。下面介绍SD卡的读写操作方式:
1. 获取SD卡的根目录
http://www.phpstudy.net/article/34296.htm
注意:不直接
1. 获取SD卡的根目录
http://www.phpstudy.net/article/34296.htm
注意:不直接
文章正文
SD卡的读写是我们在开发Android 应用程序过程中最常见的操作。下面介绍SD卡的读写操作方式:
1. 获取SD卡的根目录
http://www.phpstudy.net/article/34296.htm
注意:不直接进行读出是为了防止打文件操作对内存的消耗,所以用输入流读到硬盘上。
1 2 3 4 5 6 7 | public String readFile(String fileName) throws Exception{ FileInputStream fis = context.openFileInput(fileName); byte[] bytes = new byte[fis.available()]; fis.read(bytes); fis.close(); return new String(bytes, "utf-8" ); } |
当文件很大的时候,byte[]会占用很大的内存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package cn.itcast.fileio.service; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.os.Environment; public class SdCardService { private Context ctx; public SdCardService(Context ctx) { this.ctx = ctx; } /** * 写文件入skcard */ public void writeToSdCard(String fileName, String cont) { try { // 判断是否有挂载sdcard if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 得到sdcar文件目录 File dir = Environment.getExternalStorageDirectory(); File file = new File(dir, "itcast.txt" ); FileOutputStream fos = new FileOutputStream(file); fos.write(cont.getBytes()); fos.close(); } } catch (Exception e) { e.printStackTrace(); } } /** * 读SdCard中的文件 */ public String readSdCard(String fileName) { try { // 判断是否有挂载sdcard if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 得到sdcar文件目录 File dir = Environment.getExternalStorageDirectory(); File file = new File(dir, "itcast.txt" ); FileInputStream fis = new FileInputStream(file); return readIs2String(fis); } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将输入流数据读取到输出流当中 */ private OutputStream readIs2Os(InputStream is ,OutputStream os){ try { byte[] bytes = new byte[1024]; int length = 0 ; while ((length = is.read(bytes)) != -1){ os.write(bytes, 0, length); } is.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } return os ; } /** * 将输入流数据读取到输出流当中 */ public byte[] readIs2Bytes(InputStream is){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); readIs2Os(is,baos); return baos.toByteArray() ; } public String readIs2String(InputStream is){ try { return new String(readIs2Bytes(is), "utf-8" ); } catch (Exception e) { e.printStackTrace(); } return null ; } public String readIs2String(String fileName){ try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File dir = Environment.getExternalStorageDirectory(); File file = new File(dir,fileName); FileInputStream is = new FileInputStream(file); return readIs2String(is); } } catch (Exception e) { e.printStackTrace(); } return null ; } } |
代码注释