模块 io

该模块提供读取和写入原始字节流的功能。 它根据 CommonJS IO/A 提议实现 Stream 和 TextStream 类。

流与其他两个模块密切相关。 低级字节操作由二进制模块提供,并使用用 ByteArray 或 ByteString 类。 fs 模块返回用于读取和写入文件的 io 流。

Class MemoryStream

Instance Methods

Instance Properties

Class Stream

Instance Methods

Instance Properties

Class TextStream

Instance Methods

Instance Properties


MemoryStream (binaryOrNumber)

从内存中读取和/或写入内存中字节数组的二进制流。

如果使用 Number 参数调用构造函数,则会分配给定长度的 ByteArray,并将流的长度设置为零。

如果参数是一个 二进制对象 ,它将被用作底层缓冲区,流长度被设置为二进制对象的长度。如果参数是 ByteArray,则生成的流可读,可写和可搜索。如果它是 ByteString,则生成的流是可读且可查找的,但不可写。

如果不带参数调用,则将长度为 1024 的 ByteArray 分配为缓冲区。

Parameters

Binary|Number binaryOrNumber

the buffer to use, or the initial capacity of the buffer to allocate.


MemoryStream.prototype. close ()

关闭流,释放它所持有的资源。


MemoryStream.prototype. closed ()

如果流关闭,则返回 true,否则返回 false。

Returns

Boolean

true if the stream has been closed


MemoryStream.prototype. content

包装的缓冲区。


MemoryStream.prototype. flush ()

将写入流的字节刷新到底层介质。


MemoryStream.prototype. length

流底层缓冲区中的字节数。


MemoryStream.prototype. position

此流在包装缓冲区中的当前位置。


MemoryStream.prototype. read (maxBytes)

从流中读取最大字节数,或者直到流的末尾达到。如果未指定 maxBytes,则会读取完整的流直到达到其结尾。从已经到达结尾的流中读取将返回一个空的 ByteString。

Parameters

Number maxBytes

the maximum number of bytes to read

Returns

ByteString

MemoryStream.prototype. readInto (buffer, begin, end)

从此流读取字节到指定的缓冲区中。此方法不会增加缓冲区的长度。

Parameters

ByteArray buffer

the buffer

Number begin

optional begin index, defaults to 0.

Number end

optional end index, defaults to buffer.length - 1.

Returns

Number

The number of bytes read or -1 if the end of the stream has been reached


MemoryStream.prototype. readable ()

如果流支持读取,则返回 true,否则返回 false。 MemoryStreams 始终返回 true。

Returns

Boolean

true if stream is readable


MemoryStream.prototype. seekable ()

如果流可随机访问并支持长度和位置属性,则返回 true,否则返回 false。 MemoryStreams 始终返回 true。

Returns

Boolean

true if stream is seekable


MemoryStream.prototype. writable ()

如果流支持写入,则返回 true,否则返回 false。对于 MemoryStreams,如果包装的二进制文件是 ByteArray 的一个实例,则返回 true。

Returns

Boolean

true if stream is writable


MemoryStream.prototype. write (source, begin, end)

从 b 写入字节到这个流。如果指定了开始和结束,则只写入从开始到结束之前的范围。

Parameters

Binary source

The source to be written from

Number begin

optional

Number end

optional


Stream ()

该类实现用于读取和写入原始字节的I / O流。


Stream.prototype. close ()

关闭流,释放它所持有的资源。


Stream.prototype. closed ()

如果流已关闭则返回 true,否则返回 false。

Returns

Boolean

true if the stream has been closed


Stream.prototype. copy (output)

读取此流中可用的所有数据,并将结果写入给定的输出流,然后刷新。请注意,此功能在复制后不会关闭此流或输出流。

Parameters

Stream output

The target Stream to be written to.


Stream.prototype. flush ()

将写入流的字节刷新到底层介质。


Stream.prototype. forEach (fn, [thisObj])

读取此流中的所有数据并为读取的每个数据块调用函数 fn。回调函数是用 ByteArray 作为单个参数调用的。请注意,阅读后流不会关闭。

Parameters

Function fn

the callback function

Object [thisObj]

optional this-object to use for callback


Stream.prototype. inputStream

包装的 java.io.InputStream.


Stream.prototype. outputStream

包装的 java.io.OutputStream.


Stream.prototype. read (maxBytes)

从流中读取最大字节数,或者直到流的末尾达到。 如果未指定 maxBytes,则会读取完整的流直到达到其结尾。 从已经到达结尾的流中读取将返回一个空的 ByteString。

Parameters

Number maxBytes

the maximum number of bytes to read

Returns

ByteString

Stream.prototype. readInto (buffer, begin, end)

从此流读取字节到指定的缓冲区中。此方法不会增加缓冲区的长度。

Parameters

ByteArray buffer

the buffer

Number begin

optional begin index, defaults to 0.

Number end

optional end index, defaults to buffer.length - 1.

Returns

Number

The number of bytes read or -1 if the end of the stream has been reached


Stream.prototype. readable ()

如果流支持读取,则返回 true,否则返回 false。

Returns

Boolean

true if stream is readable


Stream.prototype. seekable ()

如果流可随机访问并支持长度和位置属性,则返回 true,否则返回 false。

Returns

Boolean

true if stream is seekable


Stream.prototype. skip (num)

尝试跳过数据流中的数字字节。如果操作无法完成,则返回跳过的acutal字节数或抛出错误。

Parameters

Number num

bytes to skip

Returns

Number

actual bytes skipped


Stream.prototype. unwrap ()

获取由此 Stream 封装的 Java 输入或输出流实例。


Stream.prototype. writable ()

如果流支持写入,则返回 true,否则返回 false。

Returns

Boolean

true if stream is writable


Stream.prototype. write (source, begin, end)

W 从 b 写入字节到这个流。如果指定了开始和结束,则只写入从开始到结束之前的范围。

Parameters

Binary source

The source to be written from

Number begin

optional

Number end

optional


TextStream (io, options, buflen)

TextStream 实现用于读写字符串的 I / O 流。它包装了一个原始的 Stream 并公开了一个类似的界面。

Parameters

Stream io

The raw Stream to be wrapped.

Object options

the options object. Supports the following properties:

  • charset: string containing the name of the encoding to use. Defaults to "utf8".
  • newline: string containing the newline character sequence to use in writeLine() and writeLines(). Defaults to "\n".
  • delimiter: string containing the delimiter to use in print(). Defaults to " ".

Number buflen

optional buffer size. Defaults to 8192.


TextStream.prototype. close ()


TextStream.prototype. content

如果包装的流是一个 MemoryStream,则包含将其内容解码为具有此流编码的字符串。否则包含一个空字符串。


TextStream.prototype. copy (output)

使用 readLine从此流读取,将结果写入目标流并刷新,直到达到此流的末尾。

Parameters

Stream output

Returns

TextStream

this stream


TextStream.prototype. flush ()


TextStream.prototype. forEach (callback, [thisObj])

使用输入流中的每一行调用回调。

Example

var txtStream = fs.open('./browserStats.csv', 'r');
txtStream.forEach(function(line) {
  console.log(line); // Print one single line
});

Parameters

Function callback

the callback function

Object [thisObj]

optional this-object to use for callback


TextStream.prototype. iterator ()

返回此流。

Returns

TextStream

this stream


TextStream.prototype. next ()

返回没有换行符的下一行输入。如果到达流的末尾,则会引发 StopIteration。

Example

var fs = require('fs');
var txtStream = fs.open('./browserStats.csv', 'r');
try {
  while (true) {
     console.log(txtStream.next());
  }
} catch (e) {
  console.log("EOF");
}

Returns

String

the next line


TextStream.prototype. print ()

将所有参数值写入一行,使用单个空白分隔值。

Example

>> var fs = require('fs');
>> var txtOutStream = fs.open('./demo.txt', 'w');
>> txtOutStream.print('foo', 'bar', 'baz');

// demo.txt content:
foo bar baz

Returns

TextStream

this stream


TextStream.prototype. raw

包装的二进制流。


TextStream.prototype. read ()

阅读完整的流,直到达到结尾并返回以字符串形式读取的数据。

Returns

String

TextStream.prototype. readInto ()

没有为 TextStream 实现。调用此方法将引发错误。


TextStream.prototype. readLine ()

从此流读取一行。如果在收集任何数据之前到达流的末尾,则返回一个空字符串。否则,返回仅包含换行符的行。回车将被丢弃。

Returns

String

the next line


TextStream.prototype. readLines ()

返回一个字符串数组,通过调用 readLine 直到它返回一个空字符串。返回的数组不包含最终的空字符串,但它在每行的末尾都包含尾随的换行符。

Example

>> var fs = require('fs');
>> var txtStream = fs.open('./sampleData.csv', 'r');
>> var lines = txtStream.readLines();
>> console.log(lines.length + ' lines');
6628 lines

Returns

Array

an array of lines


TextStream.prototype. readable ()


TextStream.prototype. seekable ()

始终返回 false,因为 TextStream 不可随机访问。


TextStream.prototype. writable ()


TextStream.prototype. write ()

将所有参数写入流。

Example

>> var fs = require('fs');
>> var txtOutStream = fs.open('./demo.txt', 'w');
>> txtOutStream.write('foo', 'bar', 'baz');

// demo.txt content:
foobarbaz

Returns

TextStream

this stream


TextStream.prototype. writeLine (line)

将给定的行写入流中,然后写入换行符。

Parameters

String line

Returns

TextStream

this stream


TextStream.prototype. writeLines (lines)

将给定的行写入流中,并用换行符终止每行。这是非标准扩展,不属于 CommonJS IO/A。

Parameters

Array lines

Returns

TextStream

this stream