在javascript中从多个文件读取数据
我在文件夹中有一组模板文件,比如在javascript中从多个文件读取数据,javascript,node.js,Javascript,Node.js,我在文件夹中有一组模板文件,比如/root/path/templates/,在路径中有相应的输入文件,比如/root/path/inputs/。此处表示保存给定模板的所有输入文件的文件夹名称 假设模板文件包含v 列表项 值为%1%2,类似于占位符,我们需要将%n替换为/root/path/inputs/中输入文件行中的行号 例如: template file name as `t1.template` and its content are `%1 %2` Input files are `i
/root/path/templates/
,在路径中有相应的输入文件,比如/root/path/inputs/
。此处
表示保存给定模板的所有输入文件的文件夹名称
假设模板文件
包含v
%1%2
,类似于占位符,我们需要将%n
替换为/root/path/inputs/
中输入文件行中的行号
例如:
template file name as `t1.template` and its content are `%1 %2`
Input files are `in1.input` and `in2.input` at path `/root/path/inputs/t1`.
The content for in1.input as:
First
Second
The content for in2.input is
Third
Fourth
My expected output should be like this:
t1/in1.output
First Second
t1/in2.output
Third Fourth
Now the template file can have any characters say:
`This is a template file with %1 %2 fields. Also it has %%1 percenage`
Here the corresponding output should be :
t1/in1.output
This is a template file with First Second fields. Also it has %1 percenage
t1/in2.output
This is a template file with Third Fourth fields. Also it has %1 percenage
means only %1 and %2 placeholders should be replaced. If %%n then while generating result show it as %n, it is not treated as placeholder.
现在,我尝试使用JS和Node实现此功能:
var fs = require('fs');
var arr = fs.readdirSync("/root/path/templates");
var response = "";
for(let i=0; i<arr.length; i++) {
var item = arr[i];
var hastemplate = item.indexOf(".template");
if(hastemplate > -1) {
var tempName = item.substring(0, hastemplate);
var lineReader = require("readline").createInterface({
input: fs.createReadStream("/root/path/inputs/"+tempName)
});
lineReader.on("line", function(line) {
console.log(line + ' ' +tempName);
});
}
}
当我试图在lineReader
中打印文件名时,我得到的文件名是t2.template
,我无法正确读取t1.template及其文件in1.input和in2.input中的数据
我还想知道如何读取输入文件并维护输出顺序
因此,我的代码不完整,我被困在这里。正如@Felix Kling在评论中所说的,这是一个范围问题,它是由以下事实触发的:
lineReader.on(“line”,…)
不同步
下面是一个以较短的方式说明错误的示例:
for (let i = 0; i < 3; i++) {
var temp = i;
setTimeout(() => console.log(i, temp), 1000);
}
第一次遇到var temp
时,会创建变量temp,然后每次循环运行时,都会修改原始变量。由于循环在任何setTimeout
调用之前结束,所有调用都将“知道”temp=2
使用let
或const
将变量绑定到每次迭代的上下文中,每次都会创建一个新变量。在这种情况下,我强烈建议使用const
来利用不变性的一些优点
const fs = require('fs');
const arr = fs.readdirSync("/root/path/templates");
let response = "";
for(let i=0; i<arr.length; i++) {
const item = arr[i];
const hastemplate = item.indexOf(".template");
if(hastemplate > -1) {
const tempName = item.substring(0, hastemplate);
const lineReader = require("readline").createInterface({
input: fs.createReadStream("/root/path/inputs/"+tempName)
});
lineReader.on("line", function(line) {
console.log(line + ' ' +tempName);
});
}
}
const fs=require('fs');
const arr=fs.readdirSync(“/root/path/templates”);
让我们回答=”;
for(设i=0;i-1){
const tempName=item.substring(0,hastemplate);
const lineReader=require(“readline”).createInterface({
输入:fs.createReadStream(“/root/path/inputs/”+tempName)
});
lineReader.on(“行”,函数(行){
console.log(行+“”+tempName);
});
}
}
这是一个范围问题(请参阅)。使用let
而不是var
。我还想知道在input:fs.createReadStream(“/root/path/inputs/”+file)
@Andre行中从哪里获得变量file
,我更新了itI,现在我一直在思考如何按照我的帖子读取相应的输入文件并显示输出消息。@学习者,你能详细说明一下吗?我认为您在理解异步编程时遇到了问题,对吗?一旦我读取了模板文件,我需要从它们的特定文件夹(如果存在)中获取输入文件,并读取和替换数据OK,这样读取部分就完成了。替换可以通过内置函数完成。那么,你到底在哪里挣扎呢?我只能读取模板文件,但由于读取是异步的,所以我一直无法理解如何读取所有输入文件并按照我的帖子维护输出消息。
0 2
1 2
2 2
const fs = require('fs');
const arr = fs.readdirSync("/root/path/templates");
let response = "";
for(let i=0; i<arr.length; i++) {
const item = arr[i];
const hastemplate = item.indexOf(".template");
if(hastemplate > -1) {
const tempName = item.substring(0, hastemplate);
const lineReader = require("readline").createInterface({
input: fs.createReadStream("/root/path/inputs/"+tempName)
});
lineReader.on("line", function(line) {
console.log(line + ' ' +tempName);
});
}
}