一、基本概念

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

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

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

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

二、例子

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

MainActivity

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

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

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

        
  1. <!--在SDCard中创建和删除文件的权限-->
  2. <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
  3. <!--往SDCard中写入数据的权限-->
  4. <uses-permissionandroid: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 获取uri的正确文件路径的办法
  2. 在android studio的虚拟机的sd卡上创建文件夹
  3. Android 使用decodeFile方法加载手机磁盘中的图片文件
  4. Android AudioRecord录音实时pcm 编码为 aac 文件
  5. android保存文件
  6. Android情景模式、文件管理器 完整示例编程详解

随机推荐

  1. Android中根据Activity的Category搜索匹
  2. android 拍照 无预览 转发
  3. android开发之获取信号强度
  4. Android之基本常见知识 getwindow() reques
  5. OnclickListener与View.OnclickListener
  6. Android 5.0新特性ToolBar
  7. android 基础学习(3)-----activity的生命
  8. Android筆記 - Dalvik的漫談
  9. Android TextView 超链接
  10. 面试篇--android下网络通讯机制(三种网络