-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusingPromisesToReadWriteAFile.js
More file actions
147 lines (93 loc) · 3.9 KB
/
usingPromisesToReadWriteAFile.js
File metadata and controls
147 lines (93 loc) · 3.9 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//NOTE : we also use Async , Await here
const fsPromises = require('fs').promises;
const path = require('path');
// Code 1 : Using Promises to read a file
const fileOps = async () => {
try{
const data = await fsPromises.readFile(path.join(__dirname,"reaadStarter.txt"),"utf-8");
console.log(data);
}
catch(err){
console.error(err);
}
}
fileOps();
//Code 2 : writing a file
// NOTE : Here we are using the const data to read and we are using the same data which has been read to be writtened in a new write file
const fileOps = async () =>{
try{
const data = await fsPromises.readFile(path.join(__dirname,"reaadStarter.txt"),"utf-8");
console.log(data);
await fsPromises.writeFile(path.join(__dirname,"promisesWriteFile.txt"),data);
console.log("Write Complete !");
}
catch(err){
console.error(err);
}
}
fileOps();
//CODE 3 : practice code for read & write
// NOTE : Here i tried to append new text by adding it to the data
const fileOps = async () =>{
try{
const data = await fsPromises.readFile(path.join(__dirname,"reaadStarter.txt"),"utf-8");
console.log(data);
await fsPromises.writeFile(path.join(__dirname,"promisesWritefile.txt"),data+"\n \n Hello this is new text that is attached to the data that is fetched from the read file !!!");
console.log("Write Complete !!!");
}
catch(error){
console.error(error);
}
}
fileOps();
//CODE 4 : Appending to the file some text
const fileOps = async () =>{
try{
const data = await fsPromises.readFile(path.join(__dirname,"reaadStarter.txt"),"utf-8");
console.log(data);
await fsPromises.writeFile(path.join(__dirname,"promisesWriteFile.txt"),data);
console.log("\nWrite complete !!");
await fsPromises.appendFile(path.join(__dirname,"promisesWriteFile.txt"),"\n \n This is a Appended text to the read data !")
console.log("\nAppend complete !!!");
}catch(err){
console.error(err);
}
};
fileOps();
//CODE 5 : renaming the file !
const fileOps = async () =>{
try{
const data = await fsPromises.readFile(path.join(__dirname,"reaadStarter.txt"),"utf-8");
console.log(data);
//Here Unlink does the Delete Operation
await fsPromises.unlink(path.join(__dirname,"reaadStarter.txt"),"utf-8");
console.log("\n Unlink Complete \n")
await fsPromises.writeFile(path.join(__dirname,"promisesWriteFile.txt"),data);
console.log("\nWrite Complete");
await fsPromises.appendFile(path.join(__dirname,"promisesWriteFile.txt"),"\n \n This is an Appended Text !!");
console.log("\nAppend complete !!");
await fsPromises.rename(path.join(__dirname,"promisesWriteFile.txt"),path.join(__dirname,"promisesRenameFile.txt"));
console.log("\nRename Complete !!\n");
// const newData = await fsPromises.readFile(path.join(__dirname,"promisesComplete.txt"),"utf-8");
// console.log(newData);
}catch(err){
console.log(err);
}
};
fileOps();
//This read & write operation below is used for working with large Files:
const fs = require ('fs');
const path = require ('path');
const rs = fs.createReadStream(path.join(__dirname,"lorem.txt"),{ encoding : "utf8"});
const ws = fs.createWriteStream(path.join(__dirname,"new-lorem.txt"));
rs.on('data',(dataChunk)=>{
ws.write(dataChunk);
console.log("Write to New File complete !!");
})
//There's a better way to use the above code more efficiently :
const fs = require('fs');
const path = require('path');
const rs = fs.createReadStream(path.join(__dirname,"lorem.txt"),{encoding:"utf-8"});
const ws = fs.createWriteStream(path.join(__dirname,"lorem2.txt"));
rs.pipe(ws);
console.log("write complete !");