createReadStream()方法是fs模块的内置应用程序编程接口,使您可以打开文件/流并读取其中的数据。
用法:
fs.createReadStream( path, options )
参数:该方法接受上述和以下所述的两个参数:
- path:此参数保存读取文件的文件路径。它可以是字符串,缓冲区或URL。
 - options:它是一个可选参数,用于保存字符串或对象。
 
返回值:此方法返回fs.ReadStream对象。
以下示例说明了Node.js中的createReadStream()方法:
范例1:
// Node.js program to demonstrate the  
// fs.createReadStream() method 
   
// Include fs module 
let fs = require('fs'), 
  
// Use fs.createReadStream() method 
// to read the file 
reader = fs.createReadStream('input.txt'); 
  
// Read and disply the file data on console 
reader.on('data', function (chunk) { 
    console.log(chunk.toString()); 
});输出:
input.txt file data: GeeksforGeeks:A computer science portal for geeks
范例2:
// Node.js program to demonstrate the  
// fs.createReadStream() method 
   
// Include fs module 
let fs = require('fs'), 
  
// Use fs.createReadStream() method 
// to read the file 
reader = fs.createReadStream('input.txt', { 
    flag:'a+', 
    encoding:'UTF-8', 
    start:5, 
    end:64, 
    highWaterMark:16 
}); 
  
// Read and disply the file data on console 
reader.on('data', function (chunk) { 
    console.log(chunk); 
});输出:
forGeeks:A comp uter science por tal for geeks
参考: https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options
相关用法
- Node.js GM whitePoint()用法及代码示例
 - Node.js GM sharpen()用法及代码示例
 - Node.js GM threshold()用法及代码示例
 - Node.js GM thumbnail()用法及代码示例
 - Node.js GM transparent()用法及代码示例
 - Node.js GM resize()用法及代码示例
 - Node.js GM drawPolyline()用法及代码示例
 - Node.js GM charcoal()用法及代码示例
 - Node.js GM drawRectangle()用法及代码示例
 - Node.js GM drawPolygon()用法及代码示例
 
注:本文由纯净天空筛选整理自Jasraj大神的英文原创作品 Node.js | fs.createReadStream() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
