-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsDemo.js
51 lines (44 loc) · 1.2 KB
/
fsDemo.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
40
41
42
43
44
45
46
47
48
49
50
51
// import fs from 'fs';
import fs from 'fs/promises';
// // readFile() - callback
// fs.readFile('./course.txt', 'utf8', (err, data) => {
// if (err) throw err;
// console.log(data);
// });
// // readFileSync() - Synchronous version
// const data = fs.readFileSync('./test.txt', 'utf8');
// console.log(data);
// // readFile() - Promisse .then()
// fs.readFile('./test.txt', 'utf8')
// .then((data) => console.log(data))
// .catch((err) => console.log(err));
// readFile() - async/await
const readFile = async () => {
try {
const data = await fs.readFile('./test.txt', 'utf8');
console.log(data);
} catch (error) {
console.log(error);
}
};
// writeFile()
const writeFile = async () => {
try {
await fs.writeFile('./test.txt', 'Hello, I am writing this file via node.js');
console.log('File writeen to...');
} catch (error) {
console.log(error);
}
};
// appendFile()
const appendFile = async () => {
try {
await fs.appendFile('./test.txt', '\nNow, I am adding this file via node.js');
console.log('File appended to...');
} catch (error) {
console.log(error);
}
};
writeFile();
appendFile();
readFile();