save text to file nodejs
fs = require('fs');
fs.writeFile(filename, data, [encoding], [callback])
save text to file nodejs
fs = require('fs');
fs.writeFile(filename, data, [encoding], [callback])
writefile in node js
// write_stream.js
const fs = require('fs');
let writeStream = fs.createWriteStream('secret.txt');
// write some data with a base64 encoding
writeStream.write('aef35ghhjdk74hja83ksnfjk888sfsf', 'base64');
// the finish event is emitted when all data has been flushed from the stream
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
// close the stream
writeStream.end();
writefile in node js
// append_file.js
const fs = require('fs');
// add a line to a lyric file, using appendFile
fs.appendFile('empirestate.txt', '\nRight there up on Broadway', (err) => {
if (err) throw err;
console.log('The lyrics were updated!');
});
writefile in node js
fs.writeFile('2pac.txt', 'Some other lyric', 'ascii', callback);
writefile in node js
// fs_write.js
const fs = require('fs');
// specify the path to the file, and create a buffer with characters we want to write
let path = 'ghetto_gospel.txt';
let buffer = new Buffer('Those who wish to follow me\nI welcome with my hands\nAnd the red sun sinks at last');
// open the file in writing mode, adding a callback function where we do the actual writing
fs.open(path, 'w', function(err, fd) {
if (err) {
throw 'could not open file: ' + err;
}
// write the contents of the buffer, from position 0 to the end, to the file descriptor returned in opening our file
fs.write(fd, buffer, 0, buffer.length, null, function(err) {
if (err) throw 'error writing file: ' + err;
fs.close(fd, function() {
console.log('wrote the file successfully');
});
});
});
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us