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

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

问题描述

我正在工作的项目(node.js)意味着文件系统的许多操作(复制/读/写等)。我想知道,哪种方法最快,我很乐意提供一些建议。

Project that I am working at (node.js) implies a lot of operations with file system (copying/reading/writing etc). I'd like to know, what methods are the fastest, and I'd be happy for some advice.

推荐答案

这是使用流在一行代码中复制文件的好方法:

This is a good way to copy a file in one line of code using streams:

var fs = require('fs');

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

在节点v8.5.0中,添加了copyFile

const fs = require('fs');

// 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中复制文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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