file-access-across-devices.md 3.0 KB
Newer Older
G
Gloria 已提交
1
# Accessing Files Across Devices
A
Annie_wang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

The distributed file system provides cross-device file access capabilities for applications. For the same application installed on multiple devices, you can implement read and write of the files in the application's distributed directory (**/data/storage/el2/distributedfiles/**) across devices by using [ohos.file.fs](app-file-access.md). For example, device A and device B are installed with the same application. After device A and device B are connected to form a Virtual Device, the application on device A can access the files of the same application on Device B. What you need to do is place the files to the distributed directory.

## How to Develop

1. Complete distributed networking for the devices.
   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 test file in the **distributedfiles/** directory on device A and write data to the file. For details about how to obtain the context in the example, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability).

   ```ts
   import fs from '@ohos.file.fs';
   
   let context =...; // Obtain the UIAbilityContext information of device A.
   let pathDir = context.distributedFilesDir;
   // Obtain the file path of the distributed directory.
   let filePath = 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 (err) {
     console.error(`Failed to openSync / writeSync / closeSync. Code: ${err.code}, message: ${err.message}`);
   }
   ```

   Read the file on device B.

   ```ts
   import fs from '@ohos.file.fs';
   
   let context =...; // Obtain the UIAbilityContext information of device B.
   let pathDir = context.distributedFilesDir;
   // Obtain the file path of the distributed directory.
   let filePath = 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 buffer = new ArrayBuffer(4096);
     // Read the file. The return value is the number of read bytes.
     let num = fs.readSync(file.fd, buffer, {
       offset: 0
     });
     // Print the read data.
     console.info('read result: ' + String.fromCharCode.apply(null, new Uint8Array(buffer.slice(0, num))));
   } catch (err) {
     console.error(`Failed to openSync / readSync. Code: ${err.code}, message: ${err.message}`);
   }
   ```