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#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.
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 createing.');
29     // Write data to the file.
30     fs.writeSync(file.fd, 'content');
31     // Close the file.
32     fs.closeSync(file.fd);
33   } catch (error: BusinessError) {
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
41   ```ts
42   import { fileIo as fs } from '@kit.CoreFileKit';
43   import { common } from '@kit.AbilityKit';
44   import { BusinessError } from '@kit.BasicServicesKit';
45   import { buffer } from '@kit.ArkTS';
46   import { distributedDeviceManager } from '@kit.DistributedServiceKit'
47 
48   // Obtain the network ID of device A.
49   let dmInstance = distributedDeviceManager.createDeviceManager("com.example.hap");
50   let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
51   let networkId = deviceInfoList[0].networkId;
52 
53   // Define the callback for accessing the user directory.
54   let listeners : fs.DfsListeners = {
55     onStatus: (networkId: string, status: number): void => {
56       console.info('Failed to access public directory');
57     }
58   }
59 
60   // Access and mount the user directory.
61   fs.connectDfs(networkId, listeners).then(() => {
62     console.info("Success to connectDfs");
63     let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device B.
64     let pathDir: string = context.distributedFilesDir;
65     // Obtain the file path of the distributed directory.
66     let filePath: string = pathDir + '/test.txt';
67   
68     try {
69       // Open the file in the distributed directory.
70       let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE);
71       // Set the buffer for receiving the read data.
72       let arrayBuffer = new ArrayBuffer(4096);
73       // Read the file. The return value is the number of read bytes.
74       class Option {
75           public offset: number = 0;
76           public length: number = 0;
77       }
78       let option = new Option();
79       option.length = arrayBuffer.byteLength;
80       let num = fs.readSync(file.fd, arrayBuffer, option);
81       // Print the read data.
82       let buf = buffer.from(arrayBuffer, 0, num);
83       console.info('read result: ' + buf.toString());
84     } catch (error: BusinessError) {
85       let err: BusinessError = error as BusinessError;
86       console.error(`Failed to openSync / readSync. Code: ${err.code}, message: ${err.message}`);
87     }
88   }).catch((error: BusinessError) => {
89     let err: BusinessError = error as BusinessError;
90     console.error(`Failed to connectDfs Code: ${err.code}, message: ${err.message}`);
91   });
92   ```
93
943. Disconnect the link for device B.
95
96   ```ts
97   import { BusinessError } from '@kit.BasicServicesKit';
98   import { distributedDeviceManager } from '@kit.DistributedServiceKit'
99   
100   // Obtain the network ID of device A.
101   let dmInstance = distributedDeviceManager.createDeviceManager("com.example.hap");
102   let deviceInfoList: Array<distributedDeviceManager.DeviceBasicInfo> = dmInstance.getAvailableDeviceListSync();
103   let networkId = deviceInfoList[0].networkId;
104 
105   // Disconnect from the user directory.
106   fs.disconnectDfs(networkId).then(() => {
107     console.info("Success to disconnectDfs");
108   }).catch((error: BusinessError) => {
109     let err: BusinessError = error as BusinessError;
110     console.error(`Failed to disconnectDfs Code: ${err.code}, message: ${err.message}`)
111   })
112   ```
113