Writing files in Node.js

前端 未结 19 2029
情歌与酒
情歌与酒 2020-11-21 11:57

I\'ve been trying to find a way to write to a file when using Node.js, but with no success. How can I do that?

19条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 12:18

     var fs = require('fs');
     fs.writeFile(path + "\\message.txt", "Hello", function(err){
     if (err) throw err;
      console.log("success");
    }); 
    

    For example : read file and write to another file :

      var fs = require('fs');
        var path = process.cwd();
        fs.readFile(path+"\\from.txt",function(err,data)
                    {
                        if(err)
                            console.log(err)
                        else
                            {
                                fs.writeFile(path+"\\to.text",function(erro){
                                    if(erro)
                                        console.log("error : "+erro);
                                    else
                                        console.log("success");
                                });
                            }
                    });
    

提交回复
热议问题