const fs = require('fs');
// Step 1: Specify the file path and content
const filePath = 'example.txt';
const fileContent = 'This is a sample file content.';
// Step 2: Use the fs.writeFile method to write content to the file
fs.writeFile(filePath, fileContent, (err) => {
// Step 3: Handle potential errors during the writing process
if (err) {
console.error('Error writing file:', err);
} else {
console.log('File written successfully.');
// Step 4: Read the file content (optional)
fs.readFile(filePath, 'utf8', (readErr, data) => {
// Step 5: Handle potential errors during the reading process (optional)
if (readErr) {
console.error('Error reading file:', readErr);
} else {
console.log('File content:', data);
}
});
}
});