• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Accessing Files Across Devices
2
3The 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.
4
5## How to Develop
6
71. Complete distributed networking for the devices.
8   Connect the devices to a LAN, and complete authentication of the devices. The devices must have the same account number.
9
102. Implement cross-device access to the files of the same application.
11   Place the files in the **distributedfiles/** directory of the application sandbox to implement access from difference devices.
12
13   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).
14
15   ```ts
16   import fs from '@ohos.file.fs';
17
18   let context =...; // Obtain the UIAbilityContext information of device A.
19   let pathDir = context.distributedFilesDir;
20   // Obtain the file path of the distributed directory.
21   let filePath = pathDir + '/test.txt';
22
23   try {
24     // Create a file in the distributed directory.
25     let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
26     console.info('Succeeded in createing.');
27     // Write data to the file.
28     fs.writeSync(file.fd, 'content');
29     // Close the file.
30     fs.closeSync(file.fd);
31   } catch (err) {
32     console.error(`Failed to openSync / writeSync / closeSync. Code: ${err.code}, message: ${err.message}`);
33   }
34   ```
35
36   Read the file on device B.
37
38   ```ts
39   import fs from '@ohos.file.fs';
40
41   let context =...; // Obtain the UIAbilityContext information of device B.
42   let pathDir = context.distributedFilesDir;
43   // Obtain the file path of the distributed directory.
44   let filePath = pathDir + '/test.txt';
45
46   try {
47     // Open the file in the distributed directory.
48     let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
49     // Set the buffer for receiving the read data.
50     let buffer = new ArrayBuffer(4096);
51     // Read the file. The return value is the number of read bytes.
52     let num = fs.readSync(file.fd, buffer, {
53       offset: 0
54     });
55     // Print the read data.
56     console.info('read result: ' + String.fromCharCode.apply(null, new Uint8Array(buffer.slice(0, num))));
57   } catch (err) {
58     console.error(`Failed to openSync / readSync. Code: ${err.code}, message: ${err.message}`);
59   }
60   ```
61