Streaming File Reading

One of the best methods to use when reading large amounts of data from a file is streaming, which opens a file as a Readable stream. As discussed in Chapter 5, you can easily implement Readable streams and link them to Writable streams by using the pipe() method. This makes is very easy to read data from a file and inject it into a Writable stream source, such as an HTTP response.

To stream data from a file asynchronously, you first need to create a Readable stream object, using the following syntax:

fs.createReadStream(path, [options])

The path parameter specifies the path to the file, which can be relative or absolute. The optional options parameter is an object that can contain encoding, mode, and flag properties that define the string encoding as well as the mode and flags used when opening the file.

After you have opened the Readable file stream, you can easily read from it by using the readable event with read() requests or by implementing a data event handler, as shown in Listing 6.8.

The code in Listing 6.8 shows how to implement a basic Readable file stream. Notice that lines 4–7 implement a data event handler that continuously reads data from the stream. Figure 6.8 shows the output of the code in Listing 6.8.

Listing 6.8 file_read_stream.js: Implementing a Readable stream to allow streaming reads from a file


01 var fs = require('fs'),
02 var options = { encoding: 'utf8', flag: 'r' };
03 var fileReadStream = fs.createReadStream("../data/grains.txt", options);
04 fileReadStream.on('data', function(chunk) {
05   console.log('Grains: %s', chunk);
06   console.log('Read %d bytes of data.', chunk.length);
07 });
08 fileReadStream.on("close", function(){
09   console.log("File Closed.");
10 });


Image

Figure 6.8 Output of file_read_stream.js, implementing streaming reads from a file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.104.250