内部存储android
内部存储是指将应用程序中的数据以文件方式存储到设备的内部存储空间中(该文件位于 data/data/<packagename>/ 目录下)。数组
通常状况下应用保存在内存下的数据其余应用是访问不了的,当您但愿确保用户或其余应用均没法访问您的文件时,内部存储是最佳选择。用户卸载该应用的同时存储在内存中的数据会一并删除。缓存
getFilesDir() :返回该应用的内部目录(data/data/<packagename>/)服务器
在应用目录里新建文件code
File file = new File(getFileDir(), filename);
应用中通常会把一些数据存入缓存中,能够减小访问服务器的次数,节省用户的流量内存
getCacheDir():返回该应用缓存文件的目录(data/data/<packagename>/cache/)get
File file = File.createTempFile(fileName, null, context.getCacheDir());
写数据it
FileOutputStream openFileOutput(String name, int mode);io
/**写文本信息到手机内存中 * @param filename : 文件名 * @param body : 文件的内容 * @throws Exception */ public void writePhone(String file, String body) throws Exception { FileOutputStream fos = null; try { fos = context.openFileOutput(file, Context.MODE_PRIVATE); fos.write(body.getBytes()); //写文本信息到手机内存中 } catch (Exception e) { if(fos != null){ fos.close(); //关闭文件输入流 } } }
其中,mode是读写文件的方式,一般使用的值有2种file
读数据
FileInputStream openFileInput(String name);
/**从手机内存中读数据 * @param file : 文件名 * @return String : 返回读到的文本信息 */ public String readPhone(String file) throws Exception { /** * 一、开辟输入流 * 二、把读取到的流数据存放到内存流中 * 三、返回读取到的信息(String的形式返回) */ FileInputStream fis = context.openFileInput(file); //字节数组输出流(内存流) ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024];//字节数组,缓存 int len = -1; while((len = fis.read(buffer)) != -1){ //把读取的内容写入到内存流中 baos.write(buffer, 0, len); } baos.close(); fis.close(); return baos.toString(); }
外部存储
外部存储是指将文件存储到一些外围设备上,如SDcard或设备内嵌的存储卡等(该文件一般位于mnt/sdcard目录下,因为手机有各类厂商生产,获取SD卡根目录一概采用Environment.getExternalStorageDirectory()这个方法)
因为外围存储设备可能被移除、丢失或者处于其余状态,因此使用外围设备以前要使用Environment.getExternalStorageState()方法来确认是否可用。由于外围存储是全局可读写的,对于无需访问限制以及您但愿与其余应用共享或容许用户使用电脑访问的文件,外部存储是最佳位置。
写数据
FileOutputStream 或 FileWriter
/**写文件到sdcard中 * @param file * @param body */ public void writeSdcard(String file, String body) throws Exception { /** * 一、判断sdcard的状态 * 二、假若有sdcard,且正常 获取sdcard的根路径,而且经过传过来的文件名进行建立或者打开该文件 * 三、写数据到输出流 * 四、关闭流 */ FileOutputStream fos = null; try{ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //取得sdcard的根路径 File rootPath = Environment.getExternalStorageDirectory(); //建立要写入sdcard的文件 File f = new File(rootPath, file); //开辟输出流 fos = new FileOutputStream(f); fos.write(body.getBytes()); }finally { if(fos != null){ fos.close(); }else{ throw new RuntimeException("sdcard状态错误"); } } }
读数据
FileInputStream 或 FileReader
/** * 读取sdcard中的文件 * @param file * @return * @throws Exception */ public String readSdcard(String file) throws Exception { FileInputStream fis = null; ByteArrayOutputStream baos = null; try{ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //取得sdcard的根目录 File rootPath = Environment.getExternalStorageDirectory(); File f = new File(rootPath.getAbsolutePath()+ "/" + file); if(f.exists()){ //判断文件是否存在 fis = new FileInputStream(f); //字节数组输出流(内存流) baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; //字节数组,缓存 int len = -1; while((len = fis.read(buffer)) != -1){ //把读取到的内容写入到内存流中 baos.write(buffer, 0, len); } }else{ return null; } }else{ throw new RuntimeException("sdcard状态错误"); } } finally{ if(baos != null){ baos.close(); } if(fis != null){ fis.close(); } } return baos.toString(); }
须要注意的是,读写外部数据时须要设置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">