当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js fs.copyFileSync()用法及代码示例


fs.copyFileSync()方法用于将文件从源路径同步复制到目标路径。如果目标中已存在该文件,Node.js将覆盖该文件。可选的mode参数可用于指定复制操作的行为。

用法:

fs.copyFileSync(src, dest, mode)

参数:此方法接受上述和以下所述的三个参数:



  • src:它是一个字符串,缓冲区或URL,表示要复制的源文件名。
  • dest:它是一个字符串,缓冲区或URL,表示复制操作将创建的目标文件名。
  • mode:它是一个整数,指定复制操作的行为。可以为这些值提供预定义的常量,这些常量具有各自的行为:
    • fs.constants.COPYFILE_EXCL:该常数指定如果目标文件名已经存在,则复制操作将失败。
    • fs.constants.COPYFILE_FICLONE:此常量指定复制操作将尝试创建copy-on-write reflink。如果平台不支持copy-on-write,则使用回退机制。
    • fs.constants.COPYFILE_FICLONE_FORCE:此常量指定复制操作将尝试创建copy-on-write reflink。如果平台不支持copy-on-write,则失败将失败,与之前的平台不同。
    • 这些常数也可以与按位OR组合以创建多个值的掩码。它是一个可选参数。该参数的默认值为0。

以下示例说明了Node.js中的fs.copyFileSync()方法:

范例1:本示例显示了“hello.txt”文件到“world.txt”文件的复制操作。

    
// Node.js program to demonstrate the 
// fs.copyFileSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Get the current filenames 
// before the function 
getCurrentFilenames(); 
console.log("\nFile Contents of hello.txt:", 
      fs.readFileSync("hello.txt", "utf8")); 
  
fs.copyFileSync("hello.txt", "world.txt"); 
  
// Get the current filenames 
// after the function 
getCurrentFilenames(); 
console.log("\nFile Contents of world.txt:", 
      fs.readFileSync("world.txt", "utf8")); 
  
// Function to get current filenames 
// in directory 
function getCurrentFilenames() { 
  console.log("\nCurrent files in directory:"); 
  fs.readdirSync(__dirname).forEach(file => { 
    console.log(file); 
  }); 
}

输出:

Current files in directory:
hello.txt
index.js

File Contents of hello.txt:Hello World

Current files in directory:
hello.txt
index.js
world.txt

File Contents of world.txt:Hello World

范例2:此示例显示了目标已存在时复制操作失败。

// Node.js program to demonstrate the 
// fs.copyFileSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
// Get the current filenames 
// before the function 
getCurrentFilenames(); 
console.log("\nFile Contents of hello.txt:", 
      fs.readFileSync("hello.txt", "utf8")); 
  
try { 
  fs.copyFileSync("hello.txt", "world.txt", 
    fs.constants.COPYFILE_EXCL); 
  
  // Get the current filenames 
  // after the function 
  getCurrentFilenames(); 
  console.log("\nFile Contents of world.txt:", 
    fs.readFileSync("hello.txt", "utf8")); 
} 
catch (err) { 
  console.log(err); 
} 
  
// Function to get current filenames 
// in directory 
function getCurrentFilenames() { 
  console.log("\nCurrent filenames:"); 
  fs.readdirSync(__dirname).forEach(file => { 
    console.log(file); 
  }); 
}

输出:

Current filenames:
hello.txt
world.txt
index.js

File Contents of hello.txt:Hello World
Error:EEXIST:file already exists, copyfile 'hello.txt' -> 'world.txt'
    at Object.copyFileSync (fs.js:1790:3)
    at Object. (G:\tutorials\nodejs-fs-copyFileSync\index.js:35:6)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  errno:-4075,
  syscall:'copyfile',
  code:'EEXIST',
  path:'hello.txt',
  dest:'world.txt'
}

参考: https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_mode




相关用法


注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.copyFileSync() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。