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

在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 "";
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值