鴻蒙OS 分布式文件服務(wù)開發(fā)指導(dǎo)

2020-09-18 17:50 更新

場景介紹

應(yīng)用可以通過分布式文件服務(wù)實現(xiàn)多個設(shè)備間的文件共享,設(shè)備 1 上的應(yīng)用A創(chuàng)建了分布式文件 a,設(shè)備 2 上的應(yīng)用A能夠通過分布式文件服務(wù)讀寫設(shè)備 1 上的文件 a。

接口說明

分布式文件兼容 POSIX 文件操作接口,應(yīng)用使用 Context.getDistributedDir() 接口獲取目錄后,可以直接使用 libc 或 JDK 訪問分布式文件。

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

開發(fā)步驟

應(yīng)用可以通過 Context.getDistributedDir() 接口獲取屬于自己的分布式目錄,然后通過 libc 或 JDK 接口,在該目錄下創(chuàng)建、刪除、讀寫文件或目錄。

  1. 設(shè)備 1 上的應(yīng)用 A 創(chuàng)建文件 hello.txt,并寫入內(nèi)容"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. 設(shè)備 2 上的應(yīng)用 A 通過 Context.getDistributedDir() 接口獲取分布式目錄。

  1. 設(shè)備 2 上的應(yīng)用 A 讀取文件 hello.txt。

   FileReader fileReader = new FileReader(filePath);
   char[] buffer = new char[1024];        
   fileReader.read(buffer);        
   fileReader.close();        
   System.out.println(buffer);
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號