nodejs使用readline逐行读取和写入文件的实现

 更新时间:2024年01月30日 10:25:06   作者:雪域迷影  
这篇文章给大家介绍了nodejs使用readline逐行读取和写入文件的实现方法,文中通过代码示例给大家讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

需求描述

今天遇到一个小的需求,就是要根据Excel中的若干列数据生成多条SQL插入脚本,类似于下面input.txt文件这样:

2	24	0	1	1	a04005	.3
2	24	0	1	2	a04006	.3
2	24	0	1	3	a04007	.3
2	16	0	1	1	a99501	.3
2	16	0	1	2	a99502	.3
2	16	0	1	3	a99500	.3
2	16	0	1	4	a99505	.3
2	16	0	1	5	a99504	.3
2	16	0	1	6	a99503	.3
2	22	0	1	23	a24901	.3
2	22	0	1	27	a24904	.3
2	22	0	1	28	a24905	.3
2	22	0	1	29	a24042	.3
2	22	0	1	36	a25002	.3
2	22	0	1	33	a24036	.3
2	22	0	1	32	a24908	.3
2	22	0	1	34	a24909	.3
2	22	0	1	35	a24910	.3
2	22	0	1	37	a24012	.3
2	22	0	1	38	a24043	.3
2	22	0	1	39	a24084	.3
2	22	0	1	40	a24911	.3
2	22	0	1	43	a25003	.3
2	22	0	1	41	a24912	.3
2	22	0	1	42	a24913	.3
2	22	0	1	44	a24070	.3
2	22	0	1	45	a25004	.3
2	22	0	1	47	a25008	.3
2	22	0	1	49	a25038	.3
2	22	0	1	48	a25006	.3
2	22	0	1	46	a24044	.3
2	22	0	1	50	a25034	.3
2	22	0	1	51	a25033	.3
2	22	0	1	52	a25902	.3
2	22	0	1	53	a25014	.3
2	22	0	1	55	a25021	.3
2	22	0	1	56	a25901	.3
2	22	0	1	57	a25019	.3
2	22	0	1	54	a24068	.3
2	22	0	1	58	a25020	.3
2	22	0	1	59	a25903	.3
2	22	0	1	60	a25904	.3
2	22	0	1	61	a24914	.3
2	22	0	1	62	a24915	.3
2	22	0	1	1	a24001	.3
2	22	0	1	2	a24045	.3
2	22	0	1	3	a24002	.3
2	22	0	1	4	a24053	.3
2	22	0	1	5	a24038	.3
2	22	0	1	6	a24037	.3
2	22	0	1	7	a24079	.3
2	22	0	1	8	a24064	.3
2	22	0	1	11	a24063	.3
2	22	0	1	12	a24902	.3
2	22	0	1	13	a24041	.3
2	22	0	1	14	a24039	.3
2	22	0	1	20	a24077	.3
2	22	0	1	18	a24074	.3
2	22	0	1	22	a24076	.3
2	22	0	1	31	a24907	.3
2	22	0	1	24	a24903	.3
2	22	0	1	25	a24011	.3
2	22	0	1	21	a24061	.3
2	22	0	1	30	a24906	.3
2	22	0	1	104	a31005	.3
2	22	0	1	206	a24072	.3
2	22	0	1	221	a05009	.3
2	22	0	1	65	a05014	.3
2	22	0	1	66	a24099	.3
2	22	0	1	67	a24046	.3
2	22	0	1	15	a24078	.3
2	22	0	1	68	a24008	.3
2	22	0	1	69	a24015	.3
2	22	0	1	70	a24916	.3
2	22	0	1	96	a31004	.3
2	22	0	1	97	a31003	.3
2	22	0	1	72	a24047	.3
2	22	0	1	71	a05013	.3
2	22	0	1	98	a31024	.3
2	22	0	1	73	a24100	.3
2	22	0	1	74	a24003	.3
2	22	0	1	101	a28006	.3
2	22	0	1	75	a24016	.3
2	22	0	1	76	a24111	.3
2	22	0	1	77	a24004	.3
2	22	0	1	78	a24018	.3
2	22	0	1	79	a24005	.3
2	22	0	1	80	a24017	.3
2	22	0	1	81	a24049	.3
2	22	0	1	82	a24027	.3
2	22	0	1	83	a24007	.3
2	22	0	1	85	a24112	.3
2	22	0	1	84	a24054	.3
2	22	0	1	86	a24019	.3
2	22	0	1	87	a24050	.3
2	22	0	1	88	a24034	.3
2	22	0	1	89	a25010	.3
2	22	0	1	90	a24009	.3
2	22	0	1	91	a24020	.3
2	22	0	1	92	a25012	.3
2	22	0	1	95	a25011	.3
2	22	0	1	215	a30008	.3
2	22	0	1	219	a99051	.3
2	22	0	1	201	a29026	.3
2	22	0	1	208	a29017	.3
2	22	0	1	106	a31025	.3
2	22	0	1	209	a24110	.3
2	22	0	1	203	a25072	.3
2	22	0	1	222	a29015	.3
2	22	0	1	202	a31030	.3
2	22	0	1	211	a31027	.3
2	22	0	1	207	a24006	.3
2	22	0	1	93	a25013	.3
2	22	0	1	94	a25068	.3
2	22	0	1	204	a25015	.3
2	22	0	1	223	a24113	.3
2	22	0	1	224	a25059	.3
2	22	0	1	9	a24919	.3
2	22	0	1	213	a30003	.3
2	17	0	1	1	a99070	.0
2	17	0	1	2	a99071	.0
2	17	0	1	3	a99072	.0
2	17	0	1	4	a99073	.0
2	17	0	1	5	a99074	.0
2	17	0	1	6	a99075	.0
2	17	0	1	7	a99076	.0
2	17	0	1	8	a99077	.0
2	17	0	1	9	a99078	.0
2	17	0	1	10	a99079	.0
2	17	0	1	11	a99080	.0
2	17	0	1	12	a99081	.0
2	17	0	1	13	a99082	.0
2	17	0	1	14	a99083	.0
2	17	0	1	15	a99084	.0
2	17	0	1	16	a99085	.0
2	17	0	1	17	a99086	.0
2	17	0	1	18	a99087	.0
2	17	0	1	19	a99088	.0
2	17	0	1	20	a99089	.0
2	17	0	1	21	a99090	.0
2	17	0	1	22	a99091	.0
2	17	0	1	23	a99092	.0
2	17	0	1	24	a99093	.0
2	17	0	1	25	a99094	.0
2	17	0	1	26	a99095	.0
2	17	0	1	27	a99096	.0
2	17	0	1	28	a99097	.0
2	17	0	1	29	a99098	.0
2	17	0	1	30	a99099	.0
2	17	0	1	31	a99100	.0
2	17	0	1	32	a99101	.0
2	17	0	1	35	a99102	.1
2	17	0	1	36	a99103	.1
2	17	0	1	37	a99104	.1

每行数据格式都是一样的,分别代表protocol, slave, number, ptype, pid,name, format,每个字段以’\t’制表符为分隔符。

上面的input.txt是我从Excel中粘贴复制过来的,本想复制到Editplus进行处理,然后生成类似于每行

INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 24, 0, 1, 1, 'a04005', '.3');

这样的SQL插入脚本。

方法一

相关Nodejs代码 app.js

直接用C++也可以写,对于nodejs读写文件不是很熟练,于是上网找资料,需要使用nodejs内置的fsreadline 库实现文件的读取和写入。得到下面的解决方案:

// app.js
var fs = require('fs');
var readline = require('readline');
var os = require('os'); 

// Node.js readline 逐行读取、写入文件内容的示例
// /article/135706.htm
// nodejs读取文件、按行读取
// https://blog.csdn.net/weixin_42171955/article/details/100156212

var strInputFileName = './input.txt';
var strOutputFileName = 'output.txt';

var fRead = fs.createReadStream(strInputFileName); 
var fWrite = fs.createWriteStream(strOutputFileName); 
  
fRead.on('end', ()=>{ 
 console.log('end'); 
}); 
  
var objReadline = readline.createInterface({ 
 input: fRead, 
 output: fWrite, 
 terminal: false
}); 

objReadline.on('line', (strLine)=>{ 
 // 获取某一行的数据,数据以\t分割,例如:24	0	1	1	a04005	.2
 var strArr = strLine.split('\t');
 let protocol = strArr[0];
 let slave = strArr[1];
 let number = strArr[2];
 let ptype = strArr[3];
 let pid = strArr[4];
 let name = strArr[5];
 let format = strArr[6];

// 由于readline::output是先写入后调用的on('line')事件, 
// 所以已经读取文件完毕时就不需要再写行号了... 
// INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
// VALUES (2, 29, 0, 1, 2, 'a06011', '.3');
let strTemp = `INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)\n `;
strTemp += `VALUES(${protocol}, ${slave}, ${number}, ${ptype}, ${pid}, '${name}', '${format}');\n`;
fWrite.write(strTemp); 
}); 
 
 
objReadline.on('close', ()=>{ 
 console.log('readline close...'); 
}); 

运行代码 node app.js

vscode_run

在当前项目目录下得到output.txt文件如下:

INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 24, 0, 1, 1, 'a04005', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 24, 0, 1, 2, 'a04006', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 24, 0, 1, 3, 'a04007', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 16, 0, 1, 1, 'a99501', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 16, 0, 1, 2, 'a99502', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 16, 0, 1, 3, 'a99500', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 16, 0, 1, 4, 'a99505', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 16, 0, 1, 5, 'a99504', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 16, 0, 1, 6, 'a99503', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 23, 'a24901', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 27, 'a24904', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 28, 'a24905', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 29, 'a24042', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 36, 'a25002', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 33, 'a24036', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 32, 'a24908', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 34, 'a24909', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 35, 'a24910', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 37, 'a24012', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 38, 'a24043', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 39, 'a24084', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 40, 'a24911', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 43, 'a25003', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 41, 'a24912', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 42, 'a24913', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 44, 'a24070', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 45, 'a25004', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 47, 'a25008', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 49, 'a25038', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 48, 'a25006', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 46, 'a24044', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 50, 'a25034', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 51, 'a25033', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 52, 'a25902', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 53, 'a25014', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 55, 'a25021', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 56, 'a25901', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 57, 'a25019', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 54, 'a24068', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 58, 'a25020', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 59, 'a25903', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 60, 'a25904', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 61, 'a24914', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 62, 'a24915', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 1, 'a24001', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 2, 'a24045', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 3, 'a24002', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 4, 'a24053', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 5, 'a24038', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 6, 'a24037', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 7, 'a24079', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 8, 'a24064', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 11, 'a24063', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 12, 'a24902', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 13, 'a24041', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 14, 'a24039', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 20, 'a24077', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 18, 'a24074', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 22, 'a24076', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 31, 'a24907', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 24, 'a24903', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 25, 'a24011', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 21, 'a24061', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 30, 'a24906', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 104, 'a31005', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 206, 'a24072', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 221, 'a05009', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 65, 'a05014', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 66, 'a24099', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 67, 'a24046', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 15, 'a24078', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 68, 'a24008', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 69, 'a24015', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 70, 'a24916', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 96, 'a31004', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 97, 'a31003', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 72, 'a24047', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 71, 'a05013', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 98, 'a31024', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 73, 'a24100', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 74, 'a24003', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 101, 'a28006', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 75, 'a24016', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 76, 'a24111', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 77, 'a24004', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 78, 'a24018', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 79, 'a24005', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 80, 'a24017', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 81, 'a24049', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 82, 'a24027', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 83, 'a24007', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 85, 'a24112', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 84, 'a24054', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 86, 'a24019', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 87, 'a24050', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 88, 'a24034', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 89, 'a25010', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 90, 'a24009', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 91, 'a24020', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 92, 'a25012', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 95, 'a25011', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 215, 'a30008', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 219, 'a99051', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 201, 'a29026', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 208, 'a29017', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 106, 'a31025', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 209, 'a24110', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 203, 'a25072', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 222, 'a29015', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 202, 'a31030', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 211, 'a31027', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 207, 'a24006', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 93, 'a25013', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 94, 'a25068', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 204, 'a25015', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 223, 'a24113', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 224, 'a25059', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 9, 'a24919', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 22, 0, 1, 213, 'a30003', '.3');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 1, 'a99070', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 2, 'a99071', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 3, 'a99072', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 4, 'a99073', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 5, 'a99074', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 6, 'a99075', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 7, 'a99076', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 8, 'a99077', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 9, 'a99078', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 10, 'a99079', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 11, 'a99080', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 12, 'a99081', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 13, 'a99082', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 14, 'a99083', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 15, 'a99084', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 16, 'a99085', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 17, 'a99086', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 18, 'a99087', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 19, 'a99088', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 20, 'a99089', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 21, 'a99090', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 22, 'a99091', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 23, 'a99092', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 24, 'a99093', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 25, 'a99094', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 26, 'a99095', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 27, 'a99096', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 28, 'a99097', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 29, 'a99098', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 30, 'a99099', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 31, 'a99100', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 32, 'a99101', '.0');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 35, 'a99102', '.1');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 36, 'a99103', '.1');
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)
 VALUES(2, 17, 0, 1, 37, 'a99104', '.1');

这样就根据input.txt文件,逐行读取并根据字段生成所需要的SQL插入脚本,然后再相应的数据库中执行就OK了。

方法二

1.实现代码

var fs = require('fs');
var readline = require('readline');

/*
* 按行读取文件内容
* 返回:字符串数组
* 参数:fReadName:文件名路径
*      callback:回调函数
* */
function readFileToArr(fReadName,callback){
    var fRead = fs.createReadStream(fReadName);
    var objReadline = readline.createInterface({
        input:fRead
    });
    var arr = new Array();
    objReadline.on('line',function (line) {
        arr.push(line);
        //console.log('line:'+ line);
    });
    objReadline.on('close',function () {
       // console.log(arr);
        callback(arr);
    });
}

2.测试例子

<1.文本文件

<2.测试程序

<3.结果

总结

到此这篇关于nodejs使用readline逐行读取和写入文件的实现的文章就介绍到这了,更多相关nodejs readline逐行读取和写入文件内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • nodejs前端自动化构建环境的搭建

    nodejs前端自动化构建环境的搭建

    本文这里给大家介绍的是nodejs中前端自动化构建环境的搭建方法,非常的细致全面,有需要的小伙伴可以参考下
    2017-07-07
  • nodejs服务内存泄露排查过程和优化方法

    nodejs服务内存泄露排查过程和优化方法

    在开发和部署Node.js应用程序时,内存泄露是一个常见的挑战,本文将探讨如何对于一个陌生项目进行内存排查和优化的方法,文章通过图文介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • Node.js JSON模块用法实例分析

    Node.js JSON模块用法实例分析

    这篇文章主要介绍了Node.js JSON模块用法,结合实例形式分析了node.js json模块的基本语法,以及使用json模块进行json格式数据解析的相关操作技巧,需要的朋友可以参考下
    2019-01-01
  • 深入理解node.js之path模块

    深入理解node.js之path模块

    本篇文章主要介绍了深入理解node.js之path模块,详细介绍了path模块的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 使用nodejs、Python写的一个简易HTTP静态文件服务器

    使用nodejs、Python写的一个简易HTTP静态文件服务器

    这篇文章主要介绍了使用nodejs、Python写的一个简易HTTP静态文件服务器,分为nodejs和Python两个版本,用类似淘宝的CSS、JS文件加载方式处理静态文件加载,需要的朋友可以参考下
    2014-07-07
  • node.js中的buffer.copy方法使用说明

    node.js中的buffer.copy方法使用说明

    这篇文章主要介绍了node.js中的buffer.copy方法使用说明,本文介绍了buffer.copy的方法说明、语法、接收参数、使用实例和实现源码,需要的朋友可以参考下
    2014-12-12
  • gulp解决跨域的配置文件问题

    gulp解决跨域的配置文件问题

    下面小编就为大家带来一篇gulp解决跨域的配置文件问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • node连接MongoDB数据库错误:MongoServerSelectionError:?connect?ECONNREFUSED?::1:27017(解决方案)

    node连接MongoDB数据库错误:MongoServerSelectionError:?connect?ECON

    使用node连接MongoDB数据库时发生报错,MongoServerSelectionError:?connect?ECONNREFUSED?::1:27017,本文给大家分享原因分析及解决方案,感兴趣的朋友跟随小编一起看看吧
    2023-04-04
  • node.js安装超详细步骤教程(推荐!)

    node.js安装超详细步骤教程(推荐!)

    其实Node.js就是运行在服务端的JavaScript,Node.js是一个基于Chrome?JavaScript运行时建立的一个平台,下面这篇文章主要给大家介绍了关于node.js安装超详细步骤教程的相关资料,需要的朋友可以参考下
    2023-06-06
  • Node.js中的文件系统(file system)模块详解

    Node.js中的文件系统(file system)模块详解

    Node.js文件系统模块提供了丰富的方法,用于读取、写入、操作文件和目录,文件系统模块是Node.js强大而灵活的一部分,为文件操作提供了方便的API,本文给大家介绍Node.js中的文件系统(file system)模块,感兴趣的朋友一起看看吧
    2023-11-11

最新评论

?


http://www.vxiaotou.com