Show List

File handling and streams

File handling and streams in Node.js allow you to work with files and data in a non-blocking, event-driven manner. This is important because it enables you to efficiently process large amounts of data without having to load the entire dataset into memory.

The fs module provides the basic file system operations in Node.js, such as reading and writing files. Here's a simple example of reading a file in Node.js:

const fs = require('fs');

fs.readFile('file.txt', 'utf-8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

In this example, the readFile method is used to read the contents of a file named file.txt. The utf-8 encoding is specified as an argument, and the method takes a callback function as its third argument, which is executed when the file has been read. The contents of the file are passed as the second argument to the callback function, and any error is passed as the first argument.

Here's a simple example of writing to a file in Node.js:

const fs = require('fs');

fs.writeFile('file.txt', 'Hello World!', (err) => {
  if (err) throw err;
  console.log('File saved!');
});

In this example, the writeFile method is used to write the string 'Hello World!' to a file named file.txt. The method takes a callback function as its third argument, which is executed when the file has been written.

Node.js streams are a powerful feature that allow you to process data in a non-blocking manner. A stream is a sequence of data that can be read from or written to over time. There are several types of streams in Node.js, including Readable streams, Writable streams, and Duplex streams.

Here's a simple example of reading from a Readable stream in Node.js:

const fs = require('fs');
const stream = fs.createReadStream('file.txt', 'utf-8');

stream.on('data', (chunk) => {
  console.log(chunk);
});

stream.on('end', () => {
  console.log('End of stream');
});

In this example, the createReadStream method is used to create a Readable stream from a file named file.txt. The data event is emitted whenever a chunk of data is available to be read, and the event listener logs the chunk to the console. The end event is emitted when there is no more data to be read, and the event listener logs a message to the console.

Here's a simple example of writing to a Writable stream in Node.js:

const fs = require('fs');
const stream = fs.createWriteStream('file.txt');

stream.write('Hello');
stream.write(' World!');
stream.end();

stream.on('finish', () => {
  console.log('Stream finished');
});
In this example, the createWriteStream method is used to create a Writable stream to a file named file.txt. The write method is used to write data to the stream, and the end method is used to indicate that there is no more data to be written. The finish event is emitted when all data has been flushed to the underlying system, and the event listener logs a message to the console.

These are just a few examples of how you can use file handling and streams in Node.js. By utilizing these features, you can build efficient, non-blocking applications that can process large amounts of data without sacrificing performance.


    Leave a Comment


  • captcha text