node.js - Node js : read two files , 比较部分文件并输出排序后的文件

标签 node.js

我刚刚启动node.js并尝试解决以下问题。 我使用了 fs.readfile 和 async 模块。但它不能正常工作。 任何人都可以向我展示示例代码吗? 预先感谢您

例如)同时读取两个大文件,并按时间将它们排序在一起。 应该会产生如下所示的输出文件

file A :

<time=100> james
<time=210> jordan
<time=300> cam
<time=500> joly
<time=700> car
..... 
 //this is a big file,  we need to handle a buffer properly

file B :

<time=90> sam
<time=210> foo
<time=350> call
<time=600> seattle
<time=660> usa
..... 
 //this is a big file, we  need to handle a buffer properly

output file :

<time=90> sam
<time=100> james
<time=210> jordan
<time=210> foo
<time=300> cam
<time=350> call
<time=500> joly
<time=600> seattle
<time=660> usa
<time=700> car
...

最佳答案

这是一个例子:

var fs = require('fs');
var Promise = require('bluebird');


var readFileAsync = function (filePath) {
    return new Promise(function (resolve, reject) {
        fs.readFile(filePath, 'utf8', function (err, data) {
            if (err) {
                return reject(err);
            }
            resolve(data);
        });

    });
};

var mergeFileData = function () {
    return Promise.join(
        readFileAsync('fileA'),
        readFileAsync('fileB'),
        function (dataA, dataB) {
            return mergeAndSortData(dataA, dataB)
        });

};

您应该自己声明 mergeAndSortData 函数。

关于node.js - Node js : read two files , 比较部分文件并输出排序后的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943132/

相关文章:

node.js - Gmail - smtp 错误 421 4.7.0

node.js - Webpack 外部和用户范围的包

javascript - Express 和 ejs 中出现错误,用于在屏幕上显示数据库中的数据

javascript - Node exec 没有执行脚本的权限

node.js - Mongoose 只返回文档的 id

node.js - 为什么当我运行 Express 服务器(React.js)时,我的浏览器显示空白页面?

node.js - 连接到 (LocalDB)\MSSQLLocalDB Sequelize

node.js - 如何向 .env 文件添加注释?

mysql - 当 usedIf 在数据库中时,sequelize findOne 找不到 userId

javascript - AJAX 调用运行 C 代码的最佳实践?