Node.js File System Operations:

Reading from and Writing to Files:

Reading from a File:

                                                    
                                                    const fs = require('fs');

// Asynchronous file reading
fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading file:', err);
        return;
    }
    console.log('File contents:', data);
});

// Synchronous file reading (avoid for large files)
try {
    const data = fs.readFileSync('example.txt', 'utf8');
    console.log('File contents:', data);
} catch (err) {
    console.error('Error reading file:', err);
}
                                                    
                                                

Writing to a File:

                                                    
                                                    const fs = require('fs');

const content = 'This is some content to write to the file.';

// Asynchronous file writing
fs.writeFile('example.txt', content, 'utf8', err => {
    if (err) {
        console.error('Error writing to file:', err);
        return;
    }
    console.log('File written successfully.');
});

// Synchronous file writing (avoid for large files)
try {
    fs.writeFileSync('example.txt', content, 'utf8');
    console.log('File written successfully.');
} catch (err) {
    console.error('Error writing to file:', err);
}
                                                    
                                                

Working with Directories:

Creating a Directory:

                                                    
                                                    const fs = require('fs');

fs.mkdir('newDirectory', err => {
    if (err) {
        console.error('Error creating directory:', err);
        return;
    }
    console.log('Directory created successfully.');
});
                                                    
                                                

Removing a Directory:

                                                    
                                                    const fs = require('fs');

fs.rmdir('directoryToRemove', err => {
    if (err) {
        console.error('Error removing directory:', err);
        return;
    }
    console.log('Directory removed successfully.');
});
                                                    
                                                

File Streams:

For instance, when working with big files, we can use file streams rather than loading everything into memory at once thereby facilitating more efficiency.

Reading from a File Stream:

                                                    
                                                    const fs = require('fs');

const readStream = fs.createReadStream('largeFile.txt', 'utf8');

readStream.on('data', chunk => {
    console.log('Received chunk:', chunk);
});

readStream.on('end', () => {
    console.log('File reading complete.');
});

readStream.on('error', err => {
    console.error('Error reading file:', err);
});
                                                    
                                                

Writing to a File Stream:

                                                    
                                                    const fs = require('fs');

const writeStream = fs.createWriteStream('output.txt', 'utf8');

writeStream.write('Data to write\n');
writeStream.write('More data to write\n');

writeStream.end('End of data');

writeStream.on('finish', () => {
    console.log('Data written to file.');
});

writeStream.on('error', err => {
    console.error('Error writing to file:', err);
});
                                                    
                                                

Node’s built-in fs module provides methods for working with the file system like reading from and writing to files, manipulating directories and managing file streams.