📜  Node.js fs.copyFileSync()函数

📅  最后修改于: 2022-05-13 01:56:43.834000             🧑  作者: Mango

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:此常量指定复制操作将尝试创建写时复制的引用链接。如果平台不支持写时复制,则使用回退机制。
    • fs.constants.COPYFILE_FICLONE_FORCE:此常量指定复制操作将尝试创建写时复制引用链接。如果平台不支持写时复制,则操作将失败,这与前一个不同。

下面的示例说明了 Node.js 中的fs.copyFileSync() 方法
示例 1:本示例展示了“hello.txt”文件到“world.txt”文件的复制操作。

javascript
// 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);
  });
}


javascript
// 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 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:此示例显示目标已存在时复制操作失败。

javascript

// 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