该模块提供读取和写入原始字节流的功能。 它根据 CommonJS IO/A 提议实现 Stream 和 TextStream 类。
流与其他两个模块密切相关。 低级字节操作由二进制模块提供,并使用用 ByteArray 或 ByteString 类。 fs 模块返回用于读取和写入文件的 io 流。
从内存中读取和/或写入内存中字节数组的二进制流。
如果使用 Number 参数调用构造函数,则会分配给定长度的 ByteArray,并将流的长度设置为零。
如果参数是一个 二进制对象 ,它将被用作底层缓冲区,流长度被设置为二进制对象的长度。如果参数是 ByteArray,则生成的流可读,可写和可搜索。如果它是 ByteString,则生成的流是可读且可查找的,但不可写。
如果不带参数调用,则将长度为 1024 的 ByteArray 分配为缓冲区。
Binary|Number | binaryOrNumber | the buffer to use, or the initial capacity of the buffer to allocate. |
关闭流,释放它所持有的资源。
如果流关闭,则返回 true,否则返回 false。
Boolean | true if the stream has been closed |
包装的缓冲区。
将写入流的字节刷新到底层介质。
流底层缓冲区中的字节数。
此流在包装缓冲区中的当前位置。
从流中读取最大字节数,或者直到流的末尾达到。如果未指定 maxBytes,则会读取完整的流直到达到其结尾。从已经到达结尾的流中读取将返回一个空的 ByteString。
Number | maxBytes | the maximum number of bytes to read |
ByteString |
从此流读取字节到指定的缓冲区中。此方法不会增加缓冲区的长度。
ByteArray | buffer | the buffer |
Number | begin | optional begin index, defaults to 0. |
Number | end | optional end index, defaults to buffer.length - 1. |
Number | The number of bytes read or -1 if the end of the stream has been reached |
如果流支持读取,则返回 true,否则返回 false。 MemoryStreams 始终返回 true。
Boolean | true if stream is readable |
如果流可随机访问并支持长度和位置属性,则返回 true,否则返回 false。 MemoryStreams 始终返回 true。
Boolean | true if stream is seekable |
如果流支持写入,则返回 true,否则返回 false。对于 MemoryStreams,如果包装的二进制文件是 ByteArray 的一个实例,则返回 true。
Boolean | true if stream is writable |
从 b 写入字节到这个流。如果指定了开始和结束,则只写入从开始到结束之前的范围。
Binary | source | The source to be written from |
Number | begin | optional |
Number | end | optional |
该类实现用于读取和写入原始字节的I / O流。
关闭流,释放它所持有的资源。
如果流已关闭则返回 true,否则返回 false。
Boolean | true if the stream has been closed |
读取此流中可用的所有数据,并将结果写入给定的输出流,然后刷新。请注意,此功能在复制后不会关闭此流或输出流。
Stream | output | The target Stream to be written to. |
将写入流的字节刷新到底层介质。
读取此流中的所有数据并为读取的每个数据块调用函数 fn。回调函数是用 ByteArray 作为单个参数调用的。请注意,阅读后流不会关闭。
Function | fn | the callback function |
Object | [thisObj] | optional this-object to use for callback |
包装的 java.io.InputStream
.
包装的 java.io.OutputStream
.
从流中读取最大字节数,或者直到流的末尾达到。 如果未指定 maxBytes,则会读取完整的流直到达到其结尾。 从已经到达结尾的流中读取将返回一个空的 ByteString。
Number | maxBytes | the maximum number of bytes to read |
ByteString |
从此流读取字节到指定的缓冲区中。此方法不会增加缓冲区的长度。
ByteArray | buffer | the buffer |
Number | begin | optional begin index, defaults to 0. |
Number | end | optional end index, defaults to buffer.length - 1. |
Number | The number of bytes read or -1 if the end of the stream has been reached |
如果流支持读取,则返回 true,否则返回 false。
Boolean | true if stream is readable |
如果流可随机访问并支持长度和位置属性,则返回 true,否则返回 false。
Boolean | true if stream is seekable |
尝试跳过数据流中的数字字节。如果操作无法完成,则返回跳过的acutal字节数或抛出错误。
Number | num | bytes to skip |
Number | actual bytes skipped |
获取由此 Stream 封装的 Java 输入或输出流实例。
如果流支持写入,则返回 true,否则返回 false。
Boolean | true if stream is writable |
W 从 b 写入字节到这个流。如果指定了开始和结束,则只写入从开始到结束之前的范围。
Binary | source | The source to be written from |
Number | begin | optional |
Number | end | optional |
TextStream 实现用于读写字符串的 I / O 流。它包装了一个原始的 Stream 并公开了一个类似的界面。
Stream | io | The raw Stream to be wrapped. |
Object | options | the options object. Supports the following properties:
|
Number | buflen | optional buffer size. Defaults to 8192. |
如果包装的流是一个 MemoryStream,则包含将其内容解码为具有此流编码的字符串。否则包含一个空字符串。
使用 readLine从此流读取,将结果写入目标流并刷新,直到达到此流的末尾。
Stream | output |
TextStream | this stream |
使用输入流中的每一行调用回调。
var txtStream = fs.open('./browserStats.csv', 'r');
txtStream.forEach(function(line) {
console.log(line); // Print one single line
});
Function | callback | the callback function |
Object | [thisObj] | optional this-object to use for callback |
返回没有换行符的下一行输入。如果到达流的末尾,则会引发 StopIteration。
var fs = require('fs');
var txtStream = fs.open('./browserStats.csv', 'r');
try {
while (true) {
console.log(txtStream.next());
}
} catch (e) {
console.log("EOF");
}
String | the next line |
将所有参数值写入一行,使用单个空白分隔值。
>> var fs = require('fs');
>> var txtOutStream = fs.open('./demo.txt', 'w');
>> txtOutStream.print('foo', 'bar', 'baz');
// demo.txt content:
foo bar baz
TextStream | this stream |
包装的二进制流。
没有为 TextStream 实现。调用此方法将引发错误。
从此流读取一行。如果在收集任何数据之前到达流的末尾,则返回一个空字符串。否则,返回仅包含换行符的行。回车将被丢弃。
String | the next line |
返回一个字符串数组,通过调用 readLine 直到它返回一个空字符串。返回的数组不包含最终的空字符串,但它在每行的末尾都包含尾随的换行符。
>> var fs = require('fs');
>> var txtStream = fs.open('./sampleData.csv', 'r');
>> var lines = txtStream.readLines();
>> console.log(lines.length + ' lines');
6628 lines
Array | an array of lines |
始终返回 false,因为 TextStream 不可随机访问。
将所有参数写入流。
>> var fs = require('fs');
>> var txtOutStream = fs.open('./demo.txt', 'w');
>> txtOutStream.write('foo', 'bar', 'baz');
// demo.txt content:
foobarbaz
TextStream | this stream |
将给定的行写入流中,然后写入换行符。
String | line |
TextStream | this stream |
将给定的行写入流中,并用换行符终止每行。这是非标准扩展,不属于 CommonJS IO/A。
Array | lines |
TextStream | this stream |