node.js, write to file
Include the node file system object.
[code language=”javascript”]
var fs = require(‘fs’);
[/code]
Write to file, will overwrite the existing content
[code language=”javascript”]
fs.writeFile(‘out.txt’, "hello, this line written by fs.writeFile function.\n", function(err){
if(err){console.log(err);}
});
[/code]
Append to file, will not overwrite the existing content
[code language=”javascript”]
fs.appendFile(‘out.txt’,"hey, this line is written by fs.appendFile function.\n", function(err){
if(err){console.log(err)}
});
[/code]
Synchronously append to file.
[code language=”javascript”]
for(var i=0; i<100000; i+=1)
{
fs.appendFileSync(‘out.txt’,"line "+i+"\n");
}
[/code]
Search within Codexpedia

Custom Search
Search the entire web

Custom Search
Related Posts