Last active
February 3, 2023 01:32
-
-
Save dudeuter/b2799d603364a670b8926689685d7014 to your computer and use it in GitHub Desktop.
writing files node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const options = { encoding: 'utf-8' }; | |
const writeStream = fs.createWriteStream('out2.txt', options); | |
const buffers = []; | |
let handled = 0; | |
function handler(error, buffer) { | |
if (error) { | |
console.error(error); | |
return; | |
} | |
handled += 1; | |
fs.writeFile('out1.txt', buffer, options, () => { | |
console.log('wrote to out1.txt'); | |
}); | |
writeStream.write(buffer, () => { | |
console.log('wrote to out2.txt\'s write stream'); | |
}); | |
buffers.push(buffer); | |
if (handled === 2) { | |
writeStream.close(() => { | |
console.log('closed file'); | |
}); | |
fs.writeFile('out3.txt', buffers.join(''), options, () => { | |
console.log('wrote to out3.txt'); | |
}); | |
} | |
} | |
fs.readFile('test1.txt', options, handler); | |
fs.readFile('test2.txt', options, handler); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello World |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
World |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment