SD卡的读写是我们在开发Android 应用程序过程中最常见的操作。下面介绍SD卡的读写操作方式:
1. 获取SD卡的根目录
2. 在SD卡上创建文件夹目录
/** * 在SD卡上创建目录 */ public File createDirOnSDCard(String dir) { File dirFile = new File(sdCardRoot + File.separator + dir +File.separator); Log.v("createDirOnSDCard", sdCardRoot + File.separator + dir +File.separator); dirFile.mkdirs(); return dirFile; }
3. 在SD卡上创建文件
/** * 在SD卡上创建文件 */ public File createFileOnSDCard(String fileName, String dir) throws IOException { File file = new File(sdCardRoot + File.separator + dir + File.separator + fileName); Log.v("createFileOnSDCard", sdCardRoot + File.separator + dir + File.separator + fileName); file.createNewFile(); return file; }
4.判断文件是否存在于SD卡的某个目录
/** * 判断SD卡上文件是否存在 */ public boolean isFileExist(String fileName, String path) { File file = new File(sdCardRoot + path + File.separator + fileName); return file.exists(); }
5.将数据写入到SD卡指定目录文件
/* 写入数据到SD卡中 */ public File writeData2SDCard(String path, String fileName, InputStream data) { File file = null; OutputStream output = null; try { createDirOnSDCard(path); //创建目录 file = createFileOnSDCard(fileName, path); //创建文件 output = new FileOutputStream(file); byte buffer[] = new byte[2*1024]; //每次写2K数据 int temp; while((temp = data.read(buffer)) != -1 ) { output.write(buffer,0,temp); } output.flush(); } catch (Exception e) { e.printStackTrace(); } finally{ try { output.close(); //关闭数据流操作 } catch (Exception e2) { e2.printStackTrace(); } } return file; }
注意事项
对SD卡的操作,必须要申请权限:
详情看这里 https://www.jb51.net/article/34296.htm
注意:不直接进行读出是为了防止打文件操作对内存的消耗,所以用输入流读到硬盘上。
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[]会占用很大的内存。
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 ; } }