filehandle.readableWebStream()
稳定性: 1 - 实验
- 返回: <ReadableStream>
返回可用于读取文件数据的 ReadableStream
。
如果多次调用此方法或在 FileHandle
关闭中或关闭后调用该方法会报错。
import {
open,
} from 'node:fs/promises';
const file = await open('./some/file/to/read');
for await (const chunk of file.readableWebStream())
console.log(chunk);
await file.close();
const {
open,
} = require('node:fs/promises');
(async () => {
const file = await open('./some/file/to/read');
for await (const chunk of file.readableWebStream())
console.log(chunk);
await file.close();
})();
虽然 ReadableStream
会读完文件,但不会自动关闭 FileHandle
。
用户代码仍然必须调用 fileHandle.close()
方法。
Stability: 1 - Experimental
- Returns: <ReadableStream>
Returns a ReadableStream
that may be used to read the files data.
An error will be thrown if this method is called more than once or is called
after the FileHandle
is closed or closing.
import {
open,
} from 'node:fs/promises';
const file = await open('./some/file/to/read');
for await (const chunk of file.readableWebStream())
console.log(chunk);
await file.close();
const {
open,
} = require('node:fs/promises');
(async () => {
const file = await open('./some/file/to/read');
for await (const chunk of file.readableWebStream())
console.log(chunk);
await file.close();
})();
While the ReadableStream
will read the file to completion, it will not
close the FileHandle
automatically. User code must still call the
fileHandle.close()
method.