标签:node,文件,fs,const,currentLine,拆分,CSV,writeStream From: https://blog.csdn.net/u012953777/article/details/136821999const fs = require('fs');
const readline = require('readline');
// 定义输入文件的路径和输出文件的目录
const inputFilePath = '大的CSV文件.csv';
const outputDirectory = 'result';
// 每个小文件包含的行数
const linesPerFile = 250000;
// 创建输出目录
if (!fs.existsSync(outputDirectory)) {
fs.mkdirSync(outputDirectory);
}
// 读取大的CSV文件
const readStream = fs.createReadStream(inputFilePath, 'utf-8');
const rl = readline.createInterface({
input: readStream,
crlfDelay: Infinity,
});
let fileCounter = 1;
let writeStream = createWriteStream();
/**创建写入流 */
function createWriteStream() {
const outputFilePath = `${outputDirectory}/小的CSV文件_${fileCounter}.csv`;
fileCounter++;
const ws = fs.createWriteStream(outputFilePath);
// 读取标题行并添加到新文件
ws.write(' \n');
return ws;
}
let currentLine = 0;
rl.on('line', (line) => {
if (currentLine > 0) {
writeStream.write(line + '\n');
}
currentLine++;
if (currentLine >= linesPerFile) {
writeStream.end();
writeStream = createWriteStream();
currentLine = 0;
}
});
rl.on('close', () => {
writeStream.end();
console.log('拆分完成!');
});