一、基本概念

在Android应用中保存文件,保存的位置有两处

①手机自带的存储空间,较小(如200M),适合保存一些小文件,Android中保存位置在data/data/应用包名/files目录

②外存储设备如SD卡,较大,适合保存大文件如视频,Android中保存位置在mnt/sdcard目录,androd1.5,android1.6保存在sdcard目录

保存的位置通过android的file explorer视图可以找到

 

二、例子

 

            
  1. package cn.xy.service;  
  2.  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7.  
  8. import android.content.Context;  
  9. import android.os.Environment;  
  10.  
  11. /**  
  12.  * 文件操作类  
  13.  *   
  14.  * @author XY  
  15.  *   
  16.  */ 
  17. public class FileService  
  18. {  
  19.     /**  
  20.      * 上下文对象  
  21.      */ 
  22.     private Context context;  
  23.  
  24.     public FileService(Context context)  
  25.     {  
  26.         super();  
  27.         this.context = context;  
  28.     }  
  29.  
  30.     /**  
  31.      * 保存文件(保存至手机自带的存储空间)  
  32.      *   
  33.      * @param filename 文件名  
  34.      * @param fileContent 文件内容  
  35.      */ 
  36.     @SuppressWarnings("static-access")  
  37.     public void save(String filename, String fileContent) throws Exception  
  38.     {  
  39.         // 私有操作模式:1.该文件只能被本应用访问。2.写入文件的内容会覆盖原有文件的内容  
  40.         FileOutputStream fos = context.openFileOutput(filename, context.MODE_PRIVATE); // 默认保存在手机自带的存储空间  
  41.         fos.write(fileContent.getBytes("UTF-8"));  
  42.         fos.close();  
  43.     }  
  44.  
  45.     /**  
  46.      * 保存文件之SD卡  
  47.      *   
  48.      * @param filename 文件名  
  49.      * @param fileContent 文件内容  
  50.      */ 
  51.     public void saveInSDCard(String filename, String fileContent) throws Exception  
  52.     {  
  53.         // 若文件被保存在SDCard中,该文件不受读写控制  
  54.         File file = new File(Environment.getExternalStorageDirectory(), filename);  
  55.         FileOutputStream fos = new FileOutputStream(file);  
  56.         fos.write(fileContent.getBytes("UTF-8"));  
  57.         fos.close();  
  58.     }  
  59.  
  60.     /**  
  61.      * 读取文件内容  
  62.      * 在创建ByteArrayOutputStream类实例时,内存中会创建一个byte数组类型的缓冲区,缓冲区会随着数据的不断写入而自动增长,可使用  
  63.      * toByteArray()和 toString()获取数据 关闭 ByteArrayOutputStream  
  64.      * 无效,此类中的方法在关闭此流后仍可被调用,而不会产生任何 IOException  
  65.      *   
  66.      * @param filename 文件名  
  67.      * @return  
  68.      */ 
  69.     public String read(String filename) throws Exception  
  70.     {  
  71.         FileInputStream fis = context.openFileInput(filename); // 默认到手机自带的存储空间去找  
  72.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  73.         byte[] buffer = new byte[1024];  
  74.         int len = 0;  
  75.         // 将内容读到buffer中,读到末尾为-1  
  76.         while ((len = fis.read(buffer)) != -1)  
  77.         {  
  78.             // 本例子将每次读到字节数组(buffer变量)内容写到内存缓冲区中,起到保存每次内容的作用  
  79.             outStream.write(buffer, 0, len);  
  80.         }  
  81.         // 取内存中保存的数据  
  82.         byte[] data = outStream.toByteArray();  
  83.         fis.close();  
  84.         String result = new String(data, "UTF-8");  
  85.         return result;  
  86.     }  
  87.  
  88. }  

MainActivity

            
  1. try 
  2. {  
  3.     // 存储在手机自带存储空间  
  4.     fs.save(filename, fileContent);  
  5.     Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();  
  6.  
  7.     // 存储在外部设备SD卡  
  8.     // 判断SDCARD是否存在和是否可读写  
  9.     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))  
  10.     {  
  11.         fs.saveInSDCard(filename, fileContent);  
  12.         Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_SHORT).show();  
  13.     }  
  14.     else 
  15.     {  
  16.         Toast.makeText(getApplicationContext(), R.string.failsdcard, Toast.LENGTH_SHORT).show();  
  17.     }  
  18.  
  19. }  
  20. catch (Exception e)  
  21. {  
  22.     Toast.makeText(getApplicationContext(), R.string.fail, Toast.LENGTH_SHORT).show();  
  23.     Log.e(tag, e.getMessage());  

文件名不带路径,直接输入如xy.txt

对于SD卡的操作,需要在AndroidManifest.xml加入权限

            
  1.  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> 
  3.  
  4.  
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

 

三、一些API

①Environment.getExternalStorageDirectory()获取的路径为mnt/sdcard目录,对于android1.5,1.6的路径是sdcard目录
②Activity中提供了两个API

getCacheDir()获取的路径为data/data/应用包名/cache目录

getFilesDir()获取的路径为data/data/应用包名/files目录

更多相关文章

  1. Android布局文件属性笔记
  2. adb设备连接以及文件拷贝
  3. Android中String资源文件的format方法
  4. android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象
  5. Android入门教程(四)之------Android工程目录结构介绍
  6. Android获取软键盘输入内容
  7. Android学习与面试重点目录

随机推荐

  1. android 左上角返回上一级的实现
  2. android中获取屏幕高宽时的注意事项
  3. 关于Android动态权限的一些疑问
  4. bluethooth BLE Android
  5. Android 网络权限配置
  6. Android中字符串的拆分-split
  7. 关于Android 混淆的内容总结
  8. Android:CoordinatorLayout使用详解
  9. Android--边距(margin)与内边距(padding)
  10. 推荐几个比较好的android客户端开发教程