-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyncmethods.js
More file actions
40 lines (32 loc) · 894 Bytes
/
syncmethods.js
File metadata and controls
40 lines (32 loc) · 894 Bytes
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
// sync method used to read the files.
// Partice we have done is all async method.
// There are both type of method avilable to read the file async and sync.
// Async Method
// Here out will first print : File Read Successfully
// Than content of file.
/*
const fs = require("fs");
fs.readFile("dummyfile.txt","utf-8",(err, data) =>
{
if(err) throw err;
console.log(data);
});
console.log("++++++++++++++");
console.log("File read successfully");
*/
// Sync method.
// In Async method we have call back but in Sync method we dont have call back.
// Better to keep it in try catch to catch the exception.
// Here first file data will print and then "File read successfully".
const fs = require("fs");
try
{
var data = fs.readFileSync("dummyfile.txt","utf-8")
console.log(data);
}
catch(e)
{
throw e;
}
console.log("++++++++++++++");
console.log("File read successfully");