在Nodejs中编写多个文件循环(Writing multiple files a loop in Nodejs)
嗨我在nodejs脚本中使用每个循环来将多个文件写入本地位置。对于courseTitleArray我正在使用“Biology 101,ruby”,我可以成功写一个文件,但不能同时写两个 。请帮助我出。
到目前为止,这是我的代码
for (var value in CourseTitleArray) { console.log( "Course Title " + CourseTitleArray[value]); var newImageLocation = path.join(__dirname, 'app/img/courseImages', CourseTitleArray[value] + ".png"); fs.readFile(image.path, function(err, data) { fs.writeFile(newImageLocation, data, function(err) { console.log(CourseTitleArray[value] + " was created successfully"); }); }); console.log("Loop executed " + value); }
在控制台中我得到以下日志。
Course Title Biology 101 Loop executed 0 Course Title ruby Loop executed 1 ruby was created successfully ruby was created successfully
似乎循环工作正常,在日志中我可以看到两个标题。 但当它写“生物学101,红宝石”已经执行了两次。
有人可以帮我解决这个问题吗? 谢谢
Hi I'm using a for each loop to in nodejs script to write multiple files to a local location.For the courseTitleArray I'm using "Biology 101,ruby" and I can write one file successfully but not the both.Please help me out.
Here is my code so far
for (var value in CourseTitleArray) { console.log( "Course Title " + CourseTitleArray[value]); var newImageLocation = path.join(__dirname, 'app/img/courseImages', CourseTitleArray[value] + ".png"); fs.readFile(image.path, function(err, data) { fs.writeFile(newImageLocation, data, function(err) { console.log(CourseTitleArray[value] + " was created successfully"); }); }); console.log("Loop executed " + value); }
And in the console I get following logs.
Course Title Biology 101 Loop executed 0 Course Title ruby Loop executed 1 ruby was created successfully ruby was created successfully
It seems the loops working fine and as in the log I can see both titles. But when it's writing "Biology 101,ruby" have executed twice .
Can anybody help me out with this? Thanks
原文:
最满意答案
请参阅上面的评论 - 您可以修改您的阅读/写作,如下所示:
using (StreamReader sr = new StreamReader(inputfile)) { using (StreamWriter writer = new StreamWriter(outputfile, false)) { int id = 1; while (sr.Peek() >= 0) { string fileLine = sr.ReadLine(); //do something with line string ttt = fileLine.Replace(" ", ", "); writer.WriteLine(ttt + "," + id); id++; } } }
See my comment above - you could modify your reading / writing as follows :
using (StreamReader sr = new StreamReader(inputfile)) { using (StreamWriter writer = new StreamWriter(outputfile, false)) { int id = 1; while (sr.Peek() >= 0) { string fileLine = sr.ReadLine(); //do something with line string ttt = fileLine.Replace(" ", ", "); writer.WriteLine(ttt + "," + id); id++; } } }
相关问答
更多-
该方法将球形坐标转换为笛卡尔坐标: private static double[] DegreesToXYZ(double radius, double theta, double phi, double[] originXYZ) { theta *= Math.PI / 180;//converting degress into radians phi *= Math.PI / 180;//converting degress into radians ...
-
通常用XSLT解决这个问题。 如果你想以编程方式创建一个csv文件,我会用Linq to Xml查询xml文件,将每个记录作为csv格式的行附加到StringBuilder实例中,最后将StringBuilders内容写入目标文件。 Normally one solves this with XSLT. If you want to create a csv file programmatically I would query the xml file with Linq to Xml append ea ...
-
Codegolf:使用C#中的最小代码将csv转换为HTML表(Codegolf: Convert csv to HTML table with smallest amount of code in C#)[2021-10-10]
将所有行读入内存 var lines =File.ReadAllLines(args[0]); using (var outfs = File.AppendText(args[1])) { outfs.Write("