3
BeginnerDay 3: File System Operations
35 minutes2 examples
Progress
0%
The File System (fs) module in Node.js provides an API for interacting with the file system. It allows you to read, write, create, delete, and manipulate files and directories.
File System Operations:
The fs module provides both synchronous and asynchronous methods for file operations. Asynchronous methods are preferred for better performance as they don't block the execution.
Reading Files:
You can read files in different ways - text files using utf8 encoding, binary files for images and videos, JSON files, and large files using streams for efficiency.
Writing Files:
File writing operations include creating new files, overwriting existing ones, appending content to existing files, saving data in JSON format, and handling large data efficiently using streams.
Directory Operations:
The fs module also provides methods for working with directories - creating directories, reading directory contents, checking if directories exist, and removing directories.
File and Directory Information:
You can get information about files and directories including size, creation date, modification date, and permissions.
Error Handling:
Always handle errors when working with the file system. Use try-catch blocks for synchronous operations and error callbacks for asynchronous operations.
File System Operations:
The fs module provides both synchronous and asynchronous methods for file operations. Asynchronous methods are preferred for better performance as they don't block the execution.
Reading Files:
You can read files in different ways - text files using utf8 encoding, binary files for images and videos, JSON files, and large files using streams for efficiency.
Writing Files:
File writing operations include creating new files, overwriting existing ones, appending content to existing files, saving data in JSON format, and handling large data efficiently using streams.
Directory Operations:
The fs module also provides methods for working with directories - creating directories, reading directory contents, checking if directories exist, and removing directories.
File and Directory Information:
You can get information about files and directories including size, creation date, modification date, and permissions.
Error Handling:
Always handle errors when working with the file system. Use try-catch blocks for synchronous operations and error callbacks for asynchronous operations.
Interactive Examples
Basic File Operations
Reading and writing files using the fs module
JavaScript
const fs = require('fs');
console.log('=== File System Operations Demo ===');
// Writing a file
const content = 'Hello, Node.js!\nThis is a test file for Day 3.\nFile system operations are awesome!';
fs.writeFileSync('demo.txt', content, 'utf8');
console.log('1. File created successfully!');
// Reading the file
const readContent = fs.readFileSync('demo.txt', 'utf8');
console.log('\nFile content:');
console.log(readContent);
// Appending to file
fs.appendFileSync('demo.txt', '\nThis line was appended!', 'utf8');
console.log('\n2. File Information:');
const stats = fs.statSync('demo.txt');
console.log('- File size:', stats.size, 'bytes');
console.log('- Created:', stats.birthtime);
console.log('- Modified:', stats.mtime);
// Reading updated content
const updatedContent = fs.readFileSync('demo.txt', 'utf8');
console.log('\nUpdated file content:');
console.log(updatedContent);Output
=== File System Operations Demo === 1. File created successfully! File content: Hello, Node.js! This is a test file for Day 3. File system operations are awesome! 2. File Information: - File size: 89 bytes - Created: Mon Jan 15 2024 10:30:00 GMT+0000 - Modified: Mon Jan 15 2024 10:30:00 GMT+0000 Updated file content: Hello, Node.js! This is a test file for Day 3. File system operations are awesome! This line was appended!
Explanation
This example demonstrates basic file operations including writing, reading, appending, and getting file information using synchronous methods.