-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathfileSystemModule.js
More file actions
23 lines (17 loc) · 1.2 KB
/
fileSystemModule.js
File metadata and controls
23 lines (17 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const fs = require('fs');
/*
Theory:
if you execture the function with Sync at the end then nodejs will stop until the function is completely executed. If Sync is not used then the next commands and operations will take place before the function.
*/
fs.readFile('fileSystemReadFile.txt', 'utf8', (error, data) => {
console.log(error, data);
});//This will read the given file.
console.log('Successfully Read file with fs.readfile()');//This appears before the content of the fs.readfile() function. because the function takes time to execute
const syncFile = fs.readFileSync('fileSystemReadFile.txt');
console.log(syncFile.toString());//This will convert the buffer string to readable string
console.log('Successfully Read file with fs.readfileSync()');//Now this will be executed after the readFileSync function only as the node js stops till the exectution of the function is done and is ready to proceed ahead.
fs.writeFile('fileSystemWriteHere.txt','Did this got written', ()=>{
console.log("Successfully wrote it!!");
});
const b = fs.writeFileSync('fileSystemWriteHere.txt', 'Did this got written with sync as well?');//This will overwritte everything present in the file.
console.log('Successfully wrote it!!');