在android 6.0以前,你可以只关注外置存储是否挂载即可,但是从6.0以后,也就是M系统后,还需要判断是否有读写权限,只有具备这些权限才可以读写外置存储。
1,Context.getFilesDir
获取路径:/data/user/0/应用包名/files
该目录是应用的文件存储目录,应用被卸载时,该目录一同被系统删除。默认存在,默认具备读写权限(6.0系统可以不用向用户申请)

2,Context.getCacheDir
获取路径:/data/user/0/应用包名/cache
该目录是应用的文件缓存目录,应用被卸载时,该目录一同被系统删除。默认存在,默认具备读写权限。不同于getFileDir,该目录下的文件在系统内存紧张时,会被清空文件,来腾出空间供系统使用,著名的图片加载库ImageLoader就是在没有外置存储读写权限时使用此文件夹。getFileDir,不会因为系统内存不足而被清空。(6.0系统可以不用向用户申请)

3,Context.getObbDir
获取路径:/storage/emulated/0/Android/obb/应用包名
该目录是应用的数据存放目录,一般被用来存放游戏数据包obb文件。默认存在,可读写(6.0系统可以不用向用户申请)

4,Context.CodeCacheDir
获取路径:/data/user/0/应用包名/code_cache
默认存在,可读写。(6.0系统可以不用向用户申请)

5,Context.getExternalFilesDir
获取路径:(以下载目录为准) /storage/emulated/0/Android/data/应用包名/files/Download
默认存在,可读写。(6.0系统可以不用向用户申请)

6,Context.getExternalCacheDir
获取路径:/storage/emulated/0/Android/data/应用包名/cache
默认存在,可读写。(6.0系统可以不用向用户申请)

7,Context.getDatabasePath
获取路径:/data/user/0/应用包名/databases/参数名
默认不存在,可读写。(6.0系统可以不用向用户申请)

8,Context.getDir
获取路径:/data/user/0/应用包名/app_参数名
默认存在,可读写。分为Private等三个权限,private代表仅能自己访问。(6.0系统可以不用向用户申请)

9,Context.getPackageCodePath
获取路径:/data/app/应用包名-1/base.apk
默认存在,获取apk包路径

10,Context.getRootDirectory
获取路径:/system
默认存在,不可读写(除非具备root权限)

11,Environment.getExternalStorageDirectory
获取路径:/storage/emulated/0
默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)

12,Environment.getExternalStoragePublicDirectory
获取路径:/storage/emulated/0/Download(以下载目录为例)
默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)

13,Environment.getDownloadCacheDirectory
获取路径:/cache
默认存在,声明权限则可读写(6.0和以后系统还需要向用户申请同意才可以)

14,Context.getFileStreamPath
获取路径:/data/data/应用包名/files/download(示例download)
该目录是应用的文件存储目录,应用被卸载时,该目录一同被系统删除。默认存在,默认具备读写权限(6.0系统可以不用向用户申请)

附注:
1)上述路径是通过getAbsulotePath方法获得,一般情况下等同于getPath
2)在6.0系统上,一般Java层实现对外置存储的文件操作需要向用户申请,如果用C层实现,则可以越过这种限制
3)配置targetsdk为19,compilesdk为22,可以避免在6.0手机上的权限限制

public static String getFilePath(Context context,String dir) {
String directoryPath="";
//判断SD卡是否可用
if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ) {
directoryPath =context.getExternalFilesDir(dir).getAbsolutePath() ;
// directoryPath =context.getExternalCacheDir().getAbsolutePath() ;
}else{
//没内存卡就存机身内存
directoryPath=context.getFilesDir()+File.separator+dir;
// directoryPath=context.getCacheDir()+File.separator+dir;
}
File file = new File(directoryPath);
if(!file.exists()){//判断文件目录是否存在
file.mkdirs();
}
LogUtil.i("filePath====>"+directoryPath);
return directoryPath;
}

RandomAccessFile

public class RandomAccessFileTest
{public static void main(String[] args) throws IOException{RandomAccessFile raf = new RandomAccessFile("d:/data.txt","rw");Person p = new Person(1001,"xiaoming",1.80d);p.write(raf);}
}
class Person
{int id;String name;double height;public Person(){}public Person(int id, String name, double height){this.id = id;this.name = name;this.height = height;}public void write(RandomAccessFile raf) throws IOException{raf.write(id);raf.writeUTF(name);raf.writeDouble(height);}
}

读取:

public class RandomAccessFileTest
{public static void main(String[] args) throws IOException{RandomAccessFile raf = new RandomAccessFile("d:/data.txt", "rw");Person p = new Person(1001, "xiaoming", 1.80d);p.write(raf);// 写入文件后,任意访问文件的指针在文件的结尾raf.seek(0);// 读取时,将指针重置到文件的开始位置。Person p2 = new Person();p2.read(raf);System.out.println("id=" + p2.getId() + ";name=" + p2.getName()+ ";height=" + p2.getHeight());}
}
class Person
{int id;String name;double height;public Person(){}public Person(int id, String name, double height){this.id = id;this.name = name;this.height = height;}public void write(RandomAccessFile raf) throws IOException{raf.writeInt(id);raf.writeUTF(name);raf.writeDouble(height);}public void read(RandomAccessFile raf) throws IOException{this.id = raf.readInt();this.name = raf.readUTF();this.height = raf.readDouble();}public int getId(){return id;}public void setId(int id){this.id = id;}public String getName(){return name;}public void setName(String name){this.name = name;}public double getHeight(){return height;}public void setHeight(double height){this.height = height;}}

追加内容

 public static void main(String[] args) throws IOException{RandomAccessFile raf = new RandomAccessFile("D:/out.txt","rw");raf.seek(raf.length());raf.write("\r\n中国移动阅读基地".getBytes());}

1、这段程序演示了在文件原有内容的基础上去追加内容。其中seek方法就是将访问指针移动到文件内容的末尾。

2、RandomAccessFile依然只能追加,不能像文件的指定位置插入内容。如果强制将文件记录指针移动到中间位置后开始输出内容,则新的内容会覆盖文件中原有的内容。

3、如果需要向文件指定的位置插入内容,程序需要先把插入点后面的内容读入缓冲区,等插入完成后,再讲缓冲区的内容追加到文件的后面。

指定位置插入

public static void main(String[] args){try{insert("d:/out.txt",5,"插入的内容");}catch (IOException e){e.printStackTrace();}}private static void insert(String fileName,long pos,String content) throws IOException{//创建临时空文件File tempFile = File.createTempFile("temp",null);//在虚拟机终止时,请求删除此抽象路径名表示的文件或目录tempFile.deleteOnExit();FileOutputStream fos = new FileOutputStream(tempFile);RandomAccessFile raf = new RandomAccessFile(fileName,"rw");raf.seek(pos);byte[] buffer = new byte[4];int num = 0;while(-1 != (num = raf.read(buffer))){fos.write(buffer,0,num);}raf.seek(pos);raf.write(content.getBytes());FileInputStream fis = new FileInputStream(tempFile);while(-1 != (num = fis.read(buffer))){raf.write(buffer,0,num);}}

例子:

public synchronized static String getUUID(Context context) {if(context != null && context.getFilesDir() != null){File installation = new File(context.getFilesDir(), "INSTALLATION");try {if (!installation.exists()){FileOutputStream out = new FileOutputStream(installation);String id = UUID.randomUUID().toString();out.write(id.getBytes());out.close();}RandomAccessFile f = new RandomAccessFile(installation, "r");byte[] bytes = new byte[(int) f.length()];f.readFully(bytes);f.close();return new String(bytes);} catch (Exception e) {e.printStackTrace();}}return "";}

Android文件目录(包含对6.0系统的说明)相关推荐

  1. android 发送广播_从0系统学Android--5.2 发送广播

    从0系统学Android--52 发送广播 本系列文章目录:更多精品文章分类 本系列持续更新中-. 初级阶段内容参考<第一行代码> 5.3 发送自定义广播 前面已经学习了如何接受广播了,下 ...

  2. nokia n9 android 4.4,安卓4.0系统加载 诺基亚N9支持双系统

    [IT168 资讯]搭载MeeGo系统的诺基亚N9可以移植部分Android程序使用早已不是什么新闻,但可以运行最新版本的Android4.0系统却听起来似乎有些不可思议.根据国外媒体的报道,来自俄罗 ...

  3. android dts配置_Android 4.0系统支持DTS音效_华为手机_手机Android频道-中关村在线

    在系统方面,华为T8950荣耀+采用的是当前新机比较主流的Android 4.0系统.虽然使用的是原生Android 4.0系统,但是华为在此基础上还是做了一定的优化.从主界面来看,并没有太多的特色. ...

  4. ps4玩android游戏,PS4更新7.0系统:手机秒变手柄,远程遥控畅玩游戏

    索尼将会在10月8日推送PS4 7.0更新,除了在派对连线人数上限从原本8人扩展至16人,并且改善连线稳定与线上聊天时的音质之外,更宣布开放更多移动设备也能加入使用Remote Play. 根据索尼的 ...

  5. vivo刷android 9教程,vivo Android9.0系统公测:速度再次提升,功能酸爽

    最近朋友的vivo NEX手机收到系统的提示,可以进行基于Android 9.0 的 Funtouch OS 4.5 系统的更新.马上进行了更新,更新之后,在手机的体验上比之前提升了不少.朋友直呼,v ...

  6. android prgoressBar setProgressDrawable 在4.0系统式正常,在2.3系统上不能正常使用的问题...

    刚开始感觉很奇怪,系统的progressBar真是个大坑,在4.0的机器上一切正常,但在2.3的机器上进度条楞是怎么也不走,被坑了几天,终于在今晚查资料看到stackoverflow上有这个问题的解决 ...

  7. 《深入解析Android 5.0系统》——第1章,第1.2节安装开发包

    本节书摘来自异步社区<深入解析Android 5.0系统>一书中的第1章,第1.2节安装开发包,作者 刘超,更多章节内容可以访问云栖社区"异步社区"公众号查看 1.2 ...

  8. 从0系统学 Android--1.1认识 Android

    一转眼工作也有几年的时间了,一直想沉下心来,再回过头来重新系统的学习一遍 Android.所以就有了这个读书笔记.俗话说温故而知新,下面就请大家再跟着我系统的学习一篇 Android 吧! 这一系列主 ...

  9. android 系统源码调试 局部变量值_如何方便快速的整编Android 9.0系统源码?

    点击上方"刘望舒",选择"星标" 多点在看,就是真爱! 作者 :  刘望舒  |  来源 :刘望舒的博客地址:http://liuwangshu.cn/fram ...

最新文章

  1. [HNOI2015]亚瑟王
  2. logback配置(与log4j对比)
  3. WebHelper类
  4. 力扣交替打印FooBar
  5. 【2016年第5期】生态经营论
  6. 用ipad同步mac的屏幕
  7. 在windows系统上安装pip的注意事项
  8. 剑指offer-21.栈的压入弹出序列
  9. knx智能照明控制系统电路图_智能照明控制系统(KNX)讲解
  10. 跨平台开源集成开发环境Eclipse
  11. 山大中心校区计算机课在哪,山东大学有几个校区,哪个校区最好及各校区介绍...
  12. 一劳永逸:服务器专用的软件防火墙
  13. 动态路由 华三nat 静态路由_史上最详细H3C路由器NAT典型配置案例
  14. 西部数据的红盘、绿盘、黑盘在物理结构和性能参数上有什么区别?
  15. android 获得ram大小,Android中获取(RAM)总运存大小跟可用运存大小
  16. 数据分析 | 岭回归与LASSO回归
  17. 用QQbot实现图灵机器人
  18. 什么是JTAG及JTAG接口简介
  19. 文件夹文件自动生成目录的方法-保存到txt
  20. 利用QEMU+GDB搭建Linux内核调试环境

热门文章

  1. css3属性之边框圆角、背景与渐变
  2. WT588F02KD-24SS语音芯片在电子烟的应用设计方案
  3. 梅西 vs 罗纳尔多:一场定义时代的竞争和世界杯荣耀的最后一枪
  4. git设置代理多账号
  5. 个人台式计算机上常用的操作系统,几个常用的操作系统简介
  6. MySQL 5.7-8.9.3 Optimizer Hints(优化器提示)
  7. 华为mate40价格 华为mate40国行版价格是多少
  8. 商品系统设计(四):商品属性设计之自定义属性
  9. vue两个按钮切换_在vue中实现多个按钮样式的点击切换?
  10. 华为第三方Linux系统好吗,华为linux系统好用吗