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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。