-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
56 lines (32 loc) · 1.26 KB
/
app.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
52
53
54
55
56
// ===== Writing ===== Writing ===== Writing =====
// Import the built-in File System (fs) module
const fs = require('fs');
// writing a file
fs.writeFile('data.txt', 'Hello world', function(err) {
if(err) throw err ;
console.log('file written')
})
// Append "New Content" with a newline to 'data.txt' file
fs.appendFile('data.txt', 'New Content \r\n', function (err) {
// Check for any errors during the file append operation
if (err) throw err;
// Log a success message when content is successfully added
console.log('Content added to file');
});
// Define the content to be added to the file
const content = "dinamic content";
// Append the content to 'data.txt', followed by a newline character
fs.appendFile('data.txt', `${content}\r\n`, function (err) {
// If an error occurs, throw it
if (err) throw err;
// Log success message when content is added
console.log('Content added to file dynamically');
});
// ===== Reading ===== Reading ===== Reading =====
// Read the contents of 'data.txt' with UTF-8 encoding
fs.readFile('data.txt', 'utf8', function(err, data) {
// If an error occurs, throw it
if (err) throw err;
// Log the file content to the console
console.log(data);
});