-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreateFile.js
47 lines (35 loc) · 1.21 KB
/
createFile.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 { writeFile, appendFile, open } = require('fs/promises');
async function writeToFile(fileName, data) {
try {
await writeFile(fileName, data);
console.log(`Wrote data to ${fileName}`);
} catch (error) {
console.error(`Got an error trying to write the file: ${error.message}`);
}
}
// Create file using the appendFile method
async function appendToFile(fileName, data) {
try {
await appendFile(fileName, data, { flag: 'w' });
console.log(`Appended data to ${fileName}`);
} catch (error) {
console.error(`Got an error trying to append the file: ${error.message}`);
}
}
// Create file using the open method
async function openFile(fileName, data) {
try {
const file = await open(fileName, 'w');
await file.write(data);
console.log(`Opened file ${fileName}`);
} catch (error) {
console.error(`Got an error trying to open the file: ${error.message}`);
}
}
// Replaces the content of the file, if the file exists.
// If the file does not exist, it creates a new file.
writeToFile('friends.txt', 'Bob');
// Create a file using the appendFile method
appendToFile('activities.txt', 'Skiing');
// Create a file with the open method
openFile('tasks.txt', 'Do homework');