nodejs使用readline逐行讀取和寫(xiě)入文件的實(shí)現(xiàn)
需求描述
今天遇到一個(gè)小的需求,就是要根據(jù)Excel中的若干列數(shù)據(jù)生成多條SQL插入腳本,類(lèi)似于下面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
每行數(shù)據(jù)格式都是一樣的,分別代表protocol, slave, number, ptype, pid,name, format,每個(gè)字段以’\t’制表符為分隔符。
上面的input.txt是我從Excel中粘貼復(fù)制過(guò)來(lái)的,本想復(fù)制到Editplus進(jìn)行處理,然后生成類(lèi)似于每行
INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format) VALUES(2, 24, 0, 1, 1, 'a04005', '.3');
這樣的SQL插入腳本。
方法一
相關(guān)Nodejs代碼 app.js
直接用C++也可以寫(xiě),對(duì)于nodejs讀寫(xiě)文件不是很熟練,于是上網(wǎng)找資料,需要使用nodejs
內(nèi)置的fs
和readline
庫(kù)實(shí)現(xiàn)文件的讀取和寫(xiě)入。得到下面的解決方案:
// app.js var fs = require('fs'); var readline = require('readline'); var os = require('os'); // Node.js readline 逐行讀取、寫(xiě)入文件內(nèi)容的示例 // http://www.dbjr.com.cn/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)=>{ // 獲取某一行的數(shù)據(jù),數(shù)據(jù)以\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是先寫(xiě)入后調(diào)用的on('line')事件, // 所以已經(jīng)讀取文件完畢時(shí)就不需要再寫(xiě)行號(hào)了... // 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...'); });
運(yùn)行代碼 node app.js
在當(dāng)前項(xiàng)目目錄下得到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');
這樣就根據(jù)input.txt文件,逐行讀取并根據(jù)字段生成所需要的SQL插入腳本,然后再相應(yīng)的數(shù)據(jù)庫(kù)中執(zhí)行就OK了。
方法二
1.實(shí)現(xiàn)代碼
var fs = require('fs'); var readline = require('readline'); /* * 按行讀取文件內(nèi)容 * 返回:字符串?dāng)?shù)組 * 參數(shù):fReadName:文件名路徑 * callback:回調(diào)函數(shù) * */ 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.測(cè)試?yán)?/h3>
<1.文本文件
<2.測(cè)試程序
<3.結(jié)果
總結(jié)
到此這篇關(guān)于nodejs使用readline逐行讀取和寫(xiě)入文件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nodejs readline逐行讀取和寫(xiě)入文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Node.js創(chuàng)建一個(gè)Express服務(wù)的方法詳解
這篇文章主要介紹了Node.js創(chuàng)建一個(gè)Express服務(wù)的方法,結(jié)合實(shí)例形式分析了node.js創(chuàng)建Express服務(wù)的具體步驟、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2020-01-01better?sqlite3安裝node?gyp原生模塊編譯prebuild-install
這篇文章主要為大家介紹了Nodejs關(guān)于原生模塊編譯node-gyp?+?prebuild-install?(以安裝?better-sqlite3為例)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Node.js、Socket.IO和GPT-4構(gòu)建AI聊天機(jī)器人的項(xiàng)目實(shí)踐
本文主要介紹了Node.js、Socket.IO和GPT-4構(gòu)建AI聊天機(jī)器人的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05NodeJS實(shí)現(xiàn)圖片上傳代碼(Express)
本篇文章主要介紹了NodeJS實(shí)現(xiàn)圖片上傳代碼(Express) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06node.js中的fs.appendFileSync方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.appendFileSync方法使用說(shuō)明,本文介紹了fs.appendFileSync方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12Nodejs實(shí)現(xiàn)爬蟲(chóng)抓取數(shù)據(jù)實(shí)例解析
這篇文章主要介紹了Nodejs實(shí)現(xiàn)爬蟲(chóng)抓取數(shù)據(jù)實(shí)例解析,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫(kù)示例詳解
這篇文章主要為大家介紹了Node服務(wù)端實(shí)戰(zhàn)之操作數(shù)據(jù)庫(kù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12