node.js & create file
node js create file if not exists
https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback
fs.open(path[, flags[, mode]], callback)
https://nodejs.org/api/fs.html#fs_file_system_flags
File System Flags
refs
https://stackoverflow.com/questions/12809068/create-an-empty-file-in-node-js
https://stackoverflow.com/questions/12899061/creating-a-file-only-if-it-doesnt-exist-in-node-js
http://www.technicalkeeda.com/nodejs-tutorials/check-if-file-folder-is-exists-or-not-using-nodejs
https://stackabuse.com/writing-to-files-in-node-js/
const fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
https://stackoverflow.com/questions/2496710/writing-files-in-node-js
fs.closeSync(fs.openSync('/var/log/my.log', 'a'));
https://www.tutorialkart.com/nodejs/create-file-in-nodejs-using-node-fs-module/
https://www.w3schools.com/nodejs/nodejs_filesystem.asp
var fs = require('fs');
var stream = fs.createWriteStream("my_file.txt");
stream.once('open', function(fd) {
stream.write("My first row
");
stream.write("My second row
");
stream.end();
})
// Just open the file and handle the error when it's not there.
function createFile(filename) {
fs.open(filename,'r',function(err, fd){
if (err) {
fs.writeFile(filename, '', function(err) {
if(err) {
console.log(err);
}
console.log("The file was saved!");
});
} else {
console.log("The file exists!");
}
});
}
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!