1# Copying Files Across Devices
2
3The distributed file system provides the cross-device file copy capability for applications. You can use [@ohos.file.fs](../reference/apis-core-file-kit/js-apis-file-fs.md) to copy files across devices and applications. This topic walks you through the process of copying a file from device A to device B.
4
5## How to Develop
6
71. Connect the devices to form a Super Device.<br>
8   Connect the devices to a LAN, and complete device authentication. The devices must be logged in with the same account.
9
102. Copy a file across devices. To copy a file of the same application across devices, place the file in the **distributedfiles/** directory of the application sandbox directory.
11
12   Copy the file of device A from the sandbox directory to the **distributedfiles/** directory of device A.
13
14   ```ts
15   import { fileIo as fs } from '@kit.CoreFileKit';
16   import { common } from '@kit.AbilityKit';
17   import { BusinessError } from '@kit.BasicServicesKit';
18   import { fileUri } from '@kit.CoreFileKit';
19
20   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device A.
21   let pathDir: string = context.filesDir;
22   let distributedPathDir: string = context.distributedFilesDir;
23   // Sandbox directory of the file to copy
24   let filePath: string = pathDir + '/src.txt';
25
26   try {
27    // If the file does not exist, create a file and write data to the file created.
28    let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
29    fs.writeSync(file.fd, 'Create file success');
30    fs.closeSync(file);
31   } catch (error) {
32    console.error(`Failed to createFile. Code: ${error.code}, message: ${error.message}`);
33   }
34
35   // Obtain the URI of the file to copy.
36   let srcUri = fileUri.getUriFromPath(filePath);
37
38   // Copy the file from the sandbox directory to the distributed file directory.
39   let destUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt');
40
41   try {
42    // Copy the file from the sandbox directory to the distributed file directory.
43    fs.copy(srcUri, destUri).then(()=>{
44      console.info("Succeeded in copying---. ");
45      console.info("src: " + srcUri + "dest: " + destUri);
46    }).catch((error: BusinessError)=>{
47      let err: BusinessError = error as BusinessError;
48      console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`);
49    })
50   } catch (error) {
51    console.error(`Failed to getData. Code: ${error.code}, message: ${error.message}`);
52   }
53   ```
54
55   Device B copies the file from the distributed file directory of device B.
56
57   ```ts
58   import { fileIo as fs } from '@kit.CoreFileKit';
59   import { common } from '@kit.AbilityKit';
60   import { BusinessError } from '@kit.BasicServicesKit';
61   import { fileUri } from '@kit.CoreFileKit';
62
63   let context = getContext(this) as common.UIAbilityContext; // Obtain the UIAbilityContext of device B.
64   let pathDir: string = context.filesDir;
65   let distributedPathDir: string = context.distributedFilesDir;
66   // Destination sandbox directory to which the file is to be copied.
67   let filePath: string = pathDir + '/dest.txt';
68
69   // Obtain the URI of the destination path.
70   let destUri = fileUri.getUriFromPath(filePath);
71
72   // Obtain the file in the distributed file directory.
73   let srcUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt');
74
75   // Define a callback for the file copy operation.
76   let progressListener: fs.ProgressListener = (progress: fs.Progress) => {
77     console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`);
78   };
79   let options: fs.CopyOptions = {
80     "progressListener" : progressListener
81   }
82
83   try {
84    // Copy the file in the distributed file directory to the destination sandbox directory.
85    fs.copy(srcUri, destUri, options).then(()=>{
86      console.info("Succeeded in copying of paste. ");
87      console.info("src: " + srcUri + "dest: " + destUri); // file://com.example.myapplication/data/storage/el2/distributedfiles/src.txt
88    }).catch((error: BusinessError)=>{
89      let err: BusinessError = error as BusinessError;
90      console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`);
91    })
92   } catch (error) {
93    console.error(`Failed to copy. Code: ${error.code}, message: ${error.message}`);
94   }
95   ```
96