接口说明
分布式文件兼容POSIX文件操作接口,应用使用Context.getDistributedDir()接口获取目录后,可以直接使用libc或JDK访问分布式文件。
接口名 |
描述 |
---|---|
Context.getDistributedDir() |
获取文件的分布式目录 |
开发步骤
应用可以通过Context.getDistributedDir()接口获取属于自己的分布式目录,然后通过libc或JDK接口,在该目录下创建、删除、读写文件或目录。
设备1上的应用A创建文件hello.txt,并写入内容”Hello World”。
Context context;
… // context初始化
File distDir = context.getDistributedDir();
String filePath = distDir + File.separator + “hello.txt”;
FileWriter fileWriter = new FileWriter(filePath, true);
fileWriter.write(“Hello World”);
fileWriter.close();
- 设备2上的应用A通过Context.getDistributedDir()接口获取分布式目录。
设备2上的应用A读取文件 hello.txt。
Context context;
… // context初始化
File distDir = context.getDistributedDir();
String filePath = distDir + File.separator + “hello.txt”;
FileReader fileReader = new FileReader(filePath);
char[] buffer = new char[1024];
fileReader.read(buffer);
fileReader.close();
System.out.println(buffer);