1e41f4b71Sopenharmony_ci# Accessing Files Across Devices
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe distributed file system provides applications the capability for accessing files across devices. If the same application has been installed on two devices, you can use the [ohos.file.fs APIs](app-file-access.md) to read and write the application files in the [distributed file directory](app-sandbox-directory.md#mapping-between-application-sandbox-paths-and-physical-paths) (**/data/storage/el2/distributedfiles/**) on the other device. For example, an application is installed on both device A and device B. 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.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## How to Develop
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci1. Connect the devices to form a Super Device.<br>
8e41f4b71Sopenharmony_ci   Perform unified account authentication for the devices. The devices are not necessarily in the same LAN.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci2. Implement cross-device access to the files of your application.<br>
11e41f4b71Sopenharmony_ci   Place the files in the **distributedfiles/** directory of the application sandbox directory to implement access from difference devices.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci   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).
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci   ```ts
16e41f4b71Sopenharmony_ci   import { fileIo as fs } from '@kit.CoreFileKit';
17e41f4b71Sopenharmony_ci   import { common } from '@kit.AbilityKit';
18e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
19e41f4b71Sopenharmony_ci 
20e41f4b71Sopenharmony_ci   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device A.
21e41f4b71Sopenharmony_ci   let pathDir: string = context.distributedFilesDir;
22e41f4b71Sopenharmony_ci   // Obtain the file path of the distributed directory.
23e41f4b71Sopenharmony_ci   let filePath: string = pathDir + '/test.txt';
24e41f4b71Sopenharmony_ci   
25e41f4b71Sopenharmony_ci   try {
26e41f4b71Sopenharmony_ci     // Create a file in the distributed directory.
27e41f4b71Sopenharmony_ci     let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
28e41f4b71Sopenharmony_ci     console.info('Succeeded in createing.');
29e41f4b71Sopenharmony_ci     // Write data to the file.
30e41f4b71Sopenharmony_ci     fs.writeSync(file.fd, 'content');
31e41f4b71Sopenharmony_ci     // Close the file.
32e41f4b71Sopenharmony_ci     fs.closeSync(file.fd);
33e41f4b71Sopenharmony_ci   } catch (error: BusinessError) {
34e41f4b71Sopenharmony_ci     let err: BusinessError = error as BusinessError;
35e41f4b71Sopenharmony_ci     console.error(`Failed to openSync / writeSync / closeSync. Code: ${err.code}, message: ${err.message}`);
36e41f4b71Sopenharmony_ci   } 
37e41f4b71Sopenharmony_ci   ```
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci   Device B initiates a link setup request to device A. After the link is set up, device B can read the test file in the distributed file directory.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci   ```ts
42e41f4b71Sopenharmony_ci   import { fileIo as fs } from '@kit.CoreFileKit';
43e41f4b71Sopenharmony_ci   import { common } from '@kit.AbilityKit';
44e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
45e41f4b71Sopenharmony_ci   import { buffer } from '@kit.ArkTS';
46e41f4b71Sopenharmony_ci   import { distributedDeviceManager } from '@kit.DistributedServiceKit'
47e41f4b71Sopenharmony_ci 
48e41f4b71Sopenharmony_ci   // Obtain the network ID of device A.
49e41f4b71Sopenharmony_ci   let dmInstance = distributedDeviceManager.createDeviceManager("com.example.hap");
50e41f4b71Sopenharmony_ci   let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
51e41f4b71Sopenharmony_ci   let networkId = deviceInfoList[0].networkId;
52e41f4b71Sopenharmony_ci 
53e41f4b71Sopenharmony_ci   // Define the callback for accessing the user directory.
54e41f4b71Sopenharmony_ci   let listeners : fs.DfsListeners = {
55e41f4b71Sopenharmony_ci     onStatus: (networkId: string, status: number): void => {
56e41f4b71Sopenharmony_ci       console.info('Failed to access public directory');
57e41f4b71Sopenharmony_ci     }
58e41f4b71Sopenharmony_ci   }
59e41f4b71Sopenharmony_ci 
60e41f4b71Sopenharmony_ci   // Access and mount the user directory.
61e41f4b71Sopenharmony_ci   fs.connectDfs(networkId, listeners).then(() => {
62e41f4b71Sopenharmony_ci     console.info("Success to connectDfs");
63e41f4b71Sopenharmony_ci     let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device B.
64e41f4b71Sopenharmony_ci     let pathDir: string = context.distributedFilesDir;
65e41f4b71Sopenharmony_ci     // Obtain the file path of the distributed directory.
66e41f4b71Sopenharmony_ci     let filePath: string = pathDir + '/test.txt';
67e41f4b71Sopenharmony_ci   
68e41f4b71Sopenharmony_ci     try {
69e41f4b71Sopenharmony_ci       // Open the file in the distributed directory.
70e41f4b71Sopenharmony_ci       let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
71e41f4b71Sopenharmony_ci       // Set the buffer for receiving the read data.
72e41f4b71Sopenharmony_ci       let arrayBuffer = new ArrayBuffer(4096);
73e41f4b71Sopenharmony_ci       // Read the file. The return value is the number of read bytes.
74e41f4b71Sopenharmony_ci       class Option {
75e41f4b71Sopenharmony_ci           public offset: number = 0;
76e41f4b71Sopenharmony_ci           public length: number = 0;
77e41f4b71Sopenharmony_ci       }
78e41f4b71Sopenharmony_ci       let option = new Option();
79e41f4b71Sopenharmony_ci       option.length = arrayBuffer.byteLength;
80e41f4b71Sopenharmony_ci       let num = fs.readSync(file.fd, arrayBuffer, option);
81e41f4b71Sopenharmony_ci       // Print the read data.
82e41f4b71Sopenharmony_ci       let buf = buffer.from(arrayBuffer, 0, num);
83e41f4b71Sopenharmony_ci       console.info('read result: ' + buf.toString());
84e41f4b71Sopenharmony_ci     } catch (error: BusinessError) {
85e41f4b71Sopenharmony_ci       let err: BusinessError = error as BusinessError;
86e41f4b71Sopenharmony_ci       console.error(`Failed to openSync / readSync. Code: ${err.code}, message: ${err.message}`);
87e41f4b71Sopenharmony_ci     }
88e41f4b71Sopenharmony_ci   }).catch((error: BusinessError) => {
89e41f4b71Sopenharmony_ci     let err: BusinessError = error as BusinessError;
90e41f4b71Sopenharmony_ci     console.error(`Failed to connectDfs Code: ${err.code}, message: ${err.message}`);
91e41f4b71Sopenharmony_ci   });
92e41f4b71Sopenharmony_ci   ```
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci3. Disconnect the link for device B.
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci   ```ts
97e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
98e41f4b71Sopenharmony_ci   import { distributedDeviceManager } from '@kit.DistributedServiceKit'
99e41f4b71Sopenharmony_ci   
100e41f4b71Sopenharmony_ci   // Obtain the network ID of device A.
101e41f4b71Sopenharmony_ci   let dmInstance = distributedDeviceManager.createDeviceManager("com.example.hap");
102e41f4b71Sopenharmony_ci   let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
103e41f4b71Sopenharmony_ci   let networkId = deviceInfoList[0].networkId;
104e41f4b71Sopenharmony_ci 
105e41f4b71Sopenharmony_ci   // Disconnect from the user directory.
106e41f4b71Sopenharmony_ci   fs.disconnectDfs(networkId).then(() => {
107e41f4b71Sopenharmony_ci     console.info("Success to disconnectDfs");
108e41f4b71Sopenharmony_ci   }).catch((error: BusinessError) => {
109e41f4b71Sopenharmony_ci     let err: BusinessError = error as BusinessError;
110e41f4b71Sopenharmony_ci     console.error(`Failed to disconnectDfs Code: ${err.code}, message: ${err.message}`)
111e41f4b71Sopenharmony_ci   })
112e41f4b71Sopenharmony_ci   ```
113