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