-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesys.js
44 lines (40 loc) · 1.3 KB
/
filesys.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
const fs = require("fs");
const fsPromises = require("fs").promises;
// fs.readFile("file.txt", function (err, data) {
// if (err) throw err;
// console.log(data.toString());
// });
const path = require("path");
const file_ops = async () => {
try {
const data = await fsPromises.readFile(
path.join(__dirname, "filesys.js"),
"utf8"
);
const data2 = await fsPromises.readFile(path.join(__dirname, "file.txt"));
// Read two file
// Combine the data now
await fsPromises.writeFile(path.join(__dirname, "out2.txt"), data);
await fsPromises.rename(
path.join(__dirname, "out2.txt"),
path.join(__dirname, "alex.txt")
);
} catch (err) {
console.log(err);
}
};
file_ops();
fs.readFile(path.join(__dirname, "file.txt"), function (err, data) {
if (err) throw err;
console.log(data.toString());
});
// In case you need to do multiple operations on a file, the async nature of these functions may cause semantic problems. To avoid that use async/await logic like in coreJS.
fs.writeFile(path.join(__dirname, "out.txt"), "Nice to meet", function (err) {
if (err) throw err;
console.log("Operatin finished");
});
process.on("uncaughtException", function (err) {
console.error("There was an uncaught error in the code");
console.log(err);
process.exit(1);
});