-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_streams.js
39 lines (30 loc) · 1012 Bytes
/
12_streams.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const fs = require('fs')
const filePath = './assets/json/data.json'
const readStream = fs.createReadStream(filePath, 'utf-8')
let fileTxt = "";
// Important : This text file being read bit by bit or Chunk by Chunk
// Reading file with streams causes your nodeJS application to use less memory because reading files in chunks
readStream.on('data', data => {
console.log(`I read ${data.length - 1} characters of text`)
fileTxt += data;
})
readStream.once('data', data => {
console.log(data)
})
// readStream.on('')
readStream.on('end', _ => {
console.log('end reading files', fileTxt.length)
})
/**
* Writeable file streams
*/
const writeStream = fs.createWriteStream('./assets/myFile.txt', 'utf-8')
const readFileStream = fs.createReadStream(filePath, 'utf-8')
// readFileStream.on('data', data => {
// writeStream.write(data)
// })
writeStream.write("hello df")
writeStream.write(" world\n")
writeStream.close()
// process.stdin.pipe(writeStream)
readFileStream.pipe(writeStream)