在 Node.js 中复制文件的最快方法 [英] Fastest way to copy a file in Node.js

查看:40
本文介绍了在 Node.js 中复制文件的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事的项目 (Node.js) 意味着对文件系统进行大量操作(复制、读取、写入等).

The project that I am working on (Node.js) implies lots of operations with the file system (copying, reading, writing, etc.).

哪种方法最快?

推荐答案

使用标准内置方式 fs.copyFile:

Use the standard built-in way fs.copyFile:

const fs = require('fs');

// File destination.txt will be created or overwritten by default.
fs.copyFile('source.txt', 'destination.txt', (err) => {
  if (err) throw err;
  console.log('source.txt was copied to destination.txt');
});

如果您必须支持旧的废弃 Node.js 版本 - 以下是在不支持 fs.copyFile 的版本中执行此操作的方法:

If you have to support old end-of-life versions of Node.js - here is how you do it in versions that do not support fs.copyFile:

const fs = require('fs');
fs.createReadStream('test.log').pipe(fs.createWriteStream('newLog.log'));

这篇关于在 Node.js 中复制文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆