harmony 鸿蒙Access Files Across Devices

  • 2023-06-24
  • 浏览 (245)

Access Files Across Devices

The distributed file system provides cross-device file access capabilities for applications. For the same application installed on multiple devices that form a Super Device, you can use ohos.file.fs APIs to implement read and write of the files in the application’s distributed directory (/data/storage/el2/distributedfiles/).

For example, device A and device B are installed with the same application. After device A and device B are connected to form a Super Device, the application on device A can access the files in the distributed directory of the same application on Device B.

How to Develop

  1. Connect the devices to form a Super Device.
    Connect the devices to a LAN, and complete authentication of the devices. The devices must have the same account number.

  2. Implement cross-device access to the files of the same application.
    Place the files in the distributedfiles/ directory of the application sandbox to implement access from difference devices.

For example, create a file in the distributedfiles/ directory on device A and write data to the file. For details about how to obtain the application context, see Obtaining the Context of UIAbility.

   import fs from '@ohos.file.fs';
   import common from '@ohos.app.ability.common';
   import { BusinessError } from '@ohos.base';

   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device A.
   let pathDir: string = context.distributedFilesDir;
   // Obtain the file path of the distributed directory.
   let filePath: string = pathDir + '/test.txt';

   try {
     // Create a file in the distributed directory.
     let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE|fs.OpenMode.CREATE);
     console.info('Succeeded in createing.');
     // Write data to the file.
     fs.writeSync(file.fd, 'content');
     // Close the file.
     fs.closeSync(file.fd);
   } catch (error) {
     let err: BusinessError = error as BusinessError;
     console.error(`Failed to openSync / writeSync / closeSync. Code: ${err.code}, message: ${err.message}`);
   }

Read the file on device B.

   import fs from '@ohos.file.fs';
   import common from '@ohos.app.ability.common';
   import buffer from '@ohos.buffer';
   import { BusinessError } from '@ohos.base';

   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device B.
   let pathDir: string = context.distributedFilesDir;
   // Obtain the file path of the distributed directory.
   let filePath: string = pathDir + '/test.txt';

   try {
     // Open the file in the distributed directory.
     let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
     // Set the buffer for receiving the read data.
     let arrayBuffer = new ArrayBuffer(4096);
     // Read the file. The return value is the number of read bytes.
     class Option {
        public offset: number = 0;
        public length: number = 0;
     }
     let option = new Option();
     option.length = arrayBuffer.byteLength;
     let num = fs.readSync(file.fd, arrayBuffer, option);
     // Print the read data.
     let buf = buffer.from(arrayBuffer, 0, num);
     console.info('read result: ' + buf.toString());
   } catch (error) {
     let err: BusinessError = error as BusinessError;
     console.error(`Failed to openSync / readSync. Code: ${err.code}, message: ${err.message}`);
   }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙File Management

harmony 鸿蒙Accessing Application Files

harmony 鸿蒙Backup and Restoration Accessed by Applications

harmony 鸿蒙Application Data Backup and Restoration Overview

harmony 鸿蒙Backup and Restoration Triggered by System Applications

harmony 鸿蒙Application File Overview

harmony 鸿蒙Uploading and Downloading an Application File

harmony 鸿蒙Obtaining Application and File System Space Statistics

harmony 鸿蒙Application Sandbox Directory

harmony 鸿蒙Developing a File Manager Application (for System Applications Only)

0  赞