fsPromises.copyFile(src, dest[, flags])
src
<string> | <Buffer> | <URL> 要复制的源文件名dest
<string> | <Buffer> | <URL> 复制操作的目标文件名flags
<number> 复制操作的修饰符。 默认值:0
。- 返回: <Promise>
异步地将 src
复制到 dest
。
默认情况下,如果 dest
已经存在,则会被覆盖。
Node.js 不保证复制操作的原子性。 如果在打开目标文件进行写入后发生错误,Node.js 将尝试删除目标文件。
flags
是可选的整数,用于指定复制操作的行为。
可以创建由两个或多个值的按位或组成的掩码(例如 fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE
)。
fs.constants.COPYFILE_EXCL
: 如果dest
已经存在,则复制操作将失败。fs.constants.COPYFILE_FICLONE
: 复制操作将尝试创建写时复制引用链接。 如果平台不支持写时复制,则使用后备复制机制。fs.constants.COPYFILE_FICLONE_FORCE
: 复制操作将尝试创建写时复制引用链接。 如果平台不支持写时复制,则该操作将失败。
const fsPromises = require('fs').promises;
// 默认情况下将创建或覆盖 destination.txt。
fsPromises.copyFile('source.txt', 'destination.txt')
.then(() => console.log('source.txt was copied to destination.txt'))
.catch(() => console.log('The file could not be copied'));
const fs = require('fs');
const fsPromises = fs.promises;
const { COPYFILE_EXCL } = fs.constants;
// 通过使用 COPYFILE_EXCL,如果 destination.txt 存在,则该操作将失败。
fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)
.then(() => console.log('source.txt was copied to destination.txt'))
.catch(() => console.log('The file could not be copied'));
src
<string> | <Buffer> | <URL> source filename to copydest
<string> | <Buffer> | <URL> destination filename of the copy operationflags
<number> modifiers for copy operation. Default:0
.- Returns: <Promise>
Asynchronously copies src
to dest
. By default, dest
is overwritten if it
already exists. The Promise
will be resolved with no arguments upon success.
Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.
flags
is an optional integer that specifies the behavior
of the copy operation. It is possible to create a mask consisting of the bitwise
OR of two or more values (e.g.
fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE
).
fs.constants.COPYFILE_EXCL
: The copy operation will fail ifdest
already exists.fs.constants.COPYFILE_FICLONE
: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.fs.constants.COPYFILE_FICLONE_FORCE
: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.
const fsPromises = require('fs').promises;
// destination.txt will be created or overwritten by default.
fsPromises.copyFile('source.txt', 'destination.txt')
.then(() => console.log('source.txt was copied to destination.txt'))
.catch(() => console.log('The file could not be copied'));
If the third argument is a number, then it specifies flags
:
const fs = require('fs');
const fsPromises = fs.promises;
const { COPYFILE_EXCL } = fs.constants;
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fsPromises.copyFile('source.txt', 'destination.txt', COPYFILE_EXCL)
.then(() => console.log('source.txt was copied to destination.txt'))
.catch(() => console.log('The file could not be copied'));