node 读取多个文件、合并多个文件、读写多个文件
一、读取文件
1、找去匹配的内容
let fs = require('fs')
let content = fs.readFileSync('/Users/**/desktop/Test.txt', 'utf-8')
console.log(content.match(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g))
2、读取文件
var fs = require('fs');
let content = fs.readFileSync('/Users/**/desktop/mark.log', 'utf-8')
fs.appendFile('/Users/caofan/desktop/Test.txt', content, function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
3、按行读取单个文件
var readline = require('readline');
var fs = require('fs');
var os = require('os');
var fReadName = '/Users/**/desktop/title/title_20171101.log';
var fWriteName = './emoji1.csv';
var fRead = fs.createReadStream(fReadName);
var fWrite = fs.createWriteStream(fWriteName);
var enableWriteIndex = true;
var objReadline = readline.createInterface({
input: fRead
});
var index = 1;
var matchArr = [];
objReadline.on('line', (line)=>{
if (enableWriteIndex) {
if(line.match(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g)){
matchArr.push(index)
var tmp = line;
fWrite.write(tmp + os.EOL);
// if (matchArr.length <= 10) { //输出匹配的前10条
// console.log(line)
// // fs.writeFileSync(fWriteName, line+ '\n', 'utf8');
// var tmp = 'line' + matchArr.length + ':' + line;
// fWrite.write(tmp + os.EOL); // 将匹配的前10条写入另一个文件
// }
}
index ++;
}
});
fRead.on('end', ()=>{
enableWriteIndex = false;
console.log(matchArr.length) // 总共匹配的条数
});
objReadline.on('close', ()=>{
});
4、 合并目录下的多个文件到另一个文件
var fs = require("fs");
var path = require('path');
//配置远程路径
var remotePath = "./emoji"; // 相对路径
//读取文件目录
fs.readdir(remotePath,function(err,files){
if(err){
console.log(err);
return;
}
files.forEach(function(filename){
var filedir = path.join(remotePath,filename);
fs.stat(filedir,function(err, stats){
if (err) throw err;
if(stats.isFile()){
if(/emoji/.test(filename)) {
console.log(filename)
let content = fs.readFileSync(path.join(remotePath,filename), 'utf-8')
fs.appendFile('/Users/caofan/desktop/Test.txt', content)
}
} else if(stats.isDirectory()){
return false
}
});
});
});
5、 读取文件目录下的所有文件,匹配内容输出到新的文件
var fs = require('fs');
var readline = require('readline');
var path = require('path');
var os = require('os');
//解析需要遍历的文件夹
var filePath = path.resolve('/Users/**/desktop/title');
//调用文件遍历方法
fileDisplay(filePath);
var fWriteName = './emojis2.csv';
var matchArr = [];
var index = 1;
function fileDisplay(filePath){
//根据文件路径读取文件,返回文件列表
fs.readdir(filePath,function(err,files){
if(err){
console.warn(err)
}else{
//遍历读取到的文件列表
files.forEach(function(filename){
//获取当前文件的绝对路径
var filedir = path.join(filePath,filename);
//根据文件路径获取文件信息,返回一个fs.Stats对象
fs.stat(filedir,function(eror,stats){
if(eror){
console.warn('获取文件stats失败');
}else{
var isFile = stats.isFile();//是文件
var isDir = stats.isDirectory();//是文件夹
if(isFile){
if(/title_201711/.test(filename)) {
var fRead = fs.createReadStream(filedir);
var fWrite = fs.createWriteStream(fWriteName);
var enableWriteIndex = true;
var objReadline = readline.createInterface({ // 按行读取
input: fRead
});
objReadline.on('line', (line)=>{
if (enableWriteIndex) {
if(/[\]\{\}<>\[@【】「」#\*\^]/.test(line)){
matchArr.push(index)
if (matchArr.length <= 10000) {
var tmp = line;
console.log(line)
fWrite.write(tmp + os.EOL);
}
}
index ++;
}
});
objReadline.on('close', ()=>{
console.log(matchArr.length)
// console.log(index)
});
}
}
if(isDir){
fileDisplay(filedir);//递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
})
});
}
});
当然,这些功能都能通过shell命令行实现。只是有时utf8编码问题导致正则在命令行匹配不到准确数据,所以在 node 中实现。