javascript - Ecrire des fichiers dans Node. js

Mots clés : javascriptnode.jsfileexpressfsjavascript

meilleur 5 Réponses javascript - Ecrire des fichiers dans Node. js

vote vote

90

const fs = require('fs');  fs.writeFile("/tmp/test", "Hey there!", function(err) {     if(err) {         return console.log(err);     }     console.log("The file was saved!"); });   // Or fs.writeFileSync('/tmp/test-sync', 'Hey there!'); 
vote vote

80

write("a") write("b") write("c") 
fs.write(new Buffer("a")) fs.write(new Buffer("b")) fs.write(new Buffer("c")) 
vote vote

80

var fs = require('fs'); var stream = fs.createWriteStream("my_file.txt"); stream.once('open', function(fd) {   stream.write("My first row\n");   stream.write("My second row\n");   stream.end(); }); 
vote vote

66

fs = require('fs');  fs.writeFileSync("foo.txt", "bar"); 
fs = require('fs');  fs.writeFile('foo.txt', 'bar', (err) => { if (err) throw err; }); 
file <string> | <Buffer> | <URL> | <integer> filename or file descriptor data <string> | <Buffer> | <Uint8Array> options <Object> | <string> callback <Function> 
fs = require('fs'); util = require('util'); writeFile = util.promisify(fs.writeFile);  fn = async () => { await writeFile('foo.txt', 'bar'); }  fn() 
vote vote

54

var path = 'public/uploads/file.txt', buffer = new Buffer("some content\n");  fs.open(path, 'w', function(err, fd) {     if (err) {         throw 'error opening file: ' + err;     }      fs.write(fd, buffer, 0, buffer.length, null, function(err) {         if (err) throw 'error writing file: ' + err;         fs.close(fd, function() {             console.log('file written');         })     }); }); 

Questions similaires