HarmonyOS-鸿蒙app开发 —基于java_数据管理_分布式文件服务开发指导

HarmonyOS-鸿蒙app开发 —基于java_数据管理_分布式文件服务开发指导

场景介绍

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

接口说明

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

表1 分布式文件服务API接口功能介绍

接口名

描述

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();

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

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x