Node.js Read and Write File

The fs module in Node.js allows you to read from and write to files. It provides both synchronous and asynchronous methods for these operations, making it flexible for different application requirements.

 

Key Features of File Read/Write

  1. Asynchronous and Synchronous Operations: Both methods allow handling files efficiently, either asynchronously (non-blocking) or synchronously (blocking).
  2. Error Handling: Proper error handling is crucial when working with files to ensure the smooth operation of applications.
  3. Text and Binary Data: You can read and write both text and binary data from/to files.

 

Reading Files

1. fs.readFile()

The fs.readFile() method is used to read the contents of a file asynchronously.

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Output:

Content of the example.txt file
  • The file is read asynchronously, and the callback function is invoked once the data is available.

2. fs.readFileSync()

The fs.readFileSync() method is a synchronous version of fs.readFile(). It blocks execution until the file is fully read.

const fs = require('fs');

const data = fs.readFileSync('example.txt', 'utf8');
console.log(data);

Output:

Content of the example.txt file
  • The method will block further code execution until the entire file is read.

 

Writing Files

1. fs.writeFile()

The fs.writeFile() method is used to write data to a file asynchronously.

const fs = require('fs');

fs.writeFile('output.txt', 'Hello, Node.js!', (err) => {
  if (err) throw err;
  console.log('File written successfully');
});

Output:

File written successfully
  • This method writes the data to the file asynchronously and invokes the callback when the operation is complete.

2. fs.writeFileSync()

The fs.writeFileSync() method is a synchronous version of fs.writeFile(). It blocks execution until the file is fully written.

const fs = require('fs');

fs.writeFileSync('output.txt', 'Hello, Node.js!');
console.log('File written successfully');

Output:

File written successfully
  • The method writes data synchronously, blocking the rest of the code until the operation completes.

3. fs.appendFile()

The fs.appendFile() method is used to append data to a file. If the file doesn't exist, it will be created.

const fs = require('fs');

fs.appendFile('output.txt', '\nAppended Text', (err) => {
  if (err) throw err;
  console.log('Data appended');
});

Output:

Data appended
  • The method appends text to the end of the file asynchronously.

 

Summary

Node.js provides efficient methods for reading from and writing to files using the fs module. You can use asynchronous methods like fs.readFile() and fs.writeFile() to avoid blocking the event loop, or synchronous methods like fs.readFileSync() and fs.writeFileSync() for simpler workflows. Appending data to files is also possible with the fs.appendFile() method. These operations are crucial for building applications that require file manipulation and management.