阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js fsPromises.copyFile

      Node.js fsPromises.copyFile(src, dest[, flags])

      新增于: v10.0.0

      • src < string> | < Buffer> | < URL> source filename to copy
      • dest < string> | < Buffer> | < URL> destination filename of the copy operation
      • flags < number> modifiers for copy operation. Default: 0.
      • Returns:

      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 if dest 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'));
      

      更多内容请参考:Node.js fs 文件系统,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录