-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsModule.js
More file actions
58 lines (48 loc) · 1.19 KB
/
fsModule.js
File metadata and controls
58 lines (48 loc) · 1.19 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// import fs from 'fs'
import { read } from 'fs'
import fs from 'fs/promises'
//readFile() - calback
// fs.readFile('./test.txt', 'utf-8', (err, data) => {
// if (err) throw err;
// console.log(data)
// })
//readFileSync - Synchronous version
// const data = fs.readFileSync('./test.txt', 'utf-8')
// console.log(data)
//readFile() - Promise .then()
// fs.readFile('./test.txt', 'utf8')
// .then((data) => console.log(data))
// .catch((err) => console.log(err))
//readFile - Promise async/await
const readFile = async () => {
try {
const data = await fs.readFile('./test.txt', 'utf-8')
console.log(data)
} catch (error) {
console.log(error)
}
}
// readFile()
// write File
const writeFile = async () => {
try {
await fs.writeFile('./test.txt', 'Hello world')
console.log('File written to')
} catch (error) {
console.log(error)
}
}
// writeFile()
// readFile()
// Append File
const appendFile = async () => {
try {
await fs.appendFile('./test.txt', '\nThis is the appended text')
console.log('File appended to')
} catch (error) {
console.log(error)
}
}
writeFile()
appendFile()
readFile()