晴川云鸿蒙OS教程:鸿蒙OS 分布式文件服务开发指导

场景介绍

应用可以通过分布式文件服务实现多个设备间的文件共享,设备 1 上的应用A创建了分布式文件 a,设备 2 上的应用A能够通过分布式文件服务读写设备 1 上的文件 a。

接口说明

分布式文件兼容 POSIX 文件操作接口,应用使用 Context.getDistributedDir() 接口获取目录后,可以直接使用 libc 或 JDK 访问分布式文件。

接口名 描述
Context.getDistributedDir() 获取文件的分布式目录

开发步骤

应用可以通过 Context.getDistributedDir() 接口获取属于自己的分布式目录,然后通过 libc 或 JDK 接口,在该目录下创建、删除、读写文件或目录。

  1. 设备 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();

  1. 设备 2 上的应用 A 通过 Context.getDistributedDir() 接口获取分布式目录。
  1. 设备 2 上的应用 A 读取文件 hello.txt。
   FileReader fileReader = new FileReader(filePath);
   char[] buffer = new char[1024];        
   fileReader.read(buffer);        
   fileReader.close();        
   System.out.println(buffer);

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
相关推荐
  • 暂无相关文章
评论 抢沙发

请登录后发表评论

    暂无评论内容