-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode3.js
More file actions
28 lines (23 loc) · 700 Bytes
/
Copy pathnode3.js
File metadata and controls
28 lines (23 loc) · 700 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
// Chapter 3 Example Code – Node.js Modules & FS
// Importing the FS module
const fs = require("fs");
// 1. Create a new file
fs.writeFile("example.txt", "This is chapter 3 content.", (err) => {
if (err) throw err;
console.log("File created!");
});
// 2. Read the file
fs.readFile("example.txt", "utf8", (err, data) => {
if (err) throw err;
console.log("File content:", data);
});
// 3. Append data to the file
fs.appendFile("example.txt", "\nAppended text.", (err) => {
if (err) throw err;
console.log("Data appended!");
});
// 4. Rename the file
fs.rename("example.txt", "chapter3-output.txt", (err) => {
if (err) throw err;
console.log("File renamed!");
});