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