首页 \ 问答 \ 在Nodejs中编写多个文件循环(Writing multiple files a loop in Nodejs)

在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


原文:
更新时间:2022-05-28 09:05

最满意答案

请参阅上面的评论 - 您可以修改您的阅读/写作,如下所示:

  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++;
      }
    }
  }

相关问答

更多