file-access-across-devices.md 3.5 KB
Newer Older
A
Annie_wang 已提交
1
# Access Files Across Devices
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
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](app-file-access.md) APIs to implement read and write of the files in the application's distributed directory (**/data/storage/el2/distributedfiles/**). 
A
Annie_wang 已提交
4

A
Annie_wang 已提交
5
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.
A
Annie_wang 已提交
6

A
Annie_wang 已提交
7
## How to Develop
A
Annie_wang 已提交
8

A
Annie_wang 已提交
9 10
1. Connect the devices to form a Super Device.
   
A
Annie_wang 已提交
11
   Connect the devices to a LAN, and complete authentication of the devices. The devices must have the same account number.
A
Annie_wang 已提交
12
2. Implement cross-device access to the files of the same application.
A
Annie_wang 已提交
13

A
Annie_wang 已提交
14 15
   Place the files in the **distributedfiles/** directory of the application sandbox to implement access from difference devices.

A
Annie_wang 已提交
16
   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](../application-models/uiability-usage.md#obtaining-the-context-of-uiability).
A
Annie_wang 已提交
17 18 19

   ```ts
   import fs from '@ohos.file.fs';
A
Annie_wang 已提交
20 21 22 23 24
   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;
A
Annie_wang 已提交
25
   // Obtain the file path of the distributed directory.
A
Annie_wang 已提交
26
   let filePath: string = pathDir + '/test.txt';
A
Annie_wang 已提交
27 28 29 30 31 32 33 34 35
   
   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);
A
Annie_wang 已提交
36 37
   } catch (error) {
     let err: BusinessError = error as BusinessError;
A
Annie_wang 已提交
38 39 40 41 42 43 44 45
     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';
A
Annie_wang 已提交
46 47 48
   import common from '@ohos.app.ability.common';
   import buffer from '@ohos.buffer';
   import { BusinessError } from '@ohos.base';
A
Annie_wang 已提交
49
   
A
Annie_wang 已提交
50 51
   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device B.
   let pathDir: string = context.distributedFilesDir;
A
Annie_wang 已提交
52
   // Obtain the file path of the distributed directory.
A
Annie_wang 已提交
53
   let filePath: string = pathDir + '/test.txt';
A
Annie_wang 已提交
54 55 56 57 58
   
   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.
A
Annie_wang 已提交
59
     let arrayBuffer = new ArrayBuffer(4096);
A
Annie_wang 已提交
60
     // Read the file. The return value is the number of read bytes.
A
Annie_wang 已提交
61 62 63 64 65 66 67
     class Option {
        public offset: number = 0;
        public length: number;
     }
     let option = new Option();
     option.length = arrayBuffer.byteLength;
     let num = fs.readSync(file.fd, arrayBuffer, option);
A
Annie_wang 已提交
68
     // Print the read data.
A
Annie_wang 已提交
69 70 71 72
     let buf = buffer.from(arrayBuffer, 0, num);
     console.info('read result: ' + buf.toString());
   } catch (error) {
     let err: BusinessError = error as BusinessError;
A
Annie_wang 已提交
73 74 75
     console.error(`Failed to openSync / readSync. Code: ${err.code}, message: ${err.message}`);
   }
   ```