File.read
从文件中读取数据。
read()
read(size)示例
分块读取文件内容。
javascript
function main() {
let file = os.open("data.txt", "r")
// data.txt
// This is a test line: Line 1.
// This is a test line: Line 2.
// ...
let chunkSize = 29
let totalBytes = 0
while (!file.eof()) {
let chunk = file.read(chunkSize)
if (chunk) {
totalBytes += chunk.length || chunk.byteLength
Log("Read chunk, total bytes so far:", totalBytes, ", chunk:", chunk)
}
}
file.close()
}返回值
| 类型 | 描述 |
string / ArrayBuffer | 返回读取的内容。已到达文件末尾时返回空字符串 |
参数
| 名称 | 类型 | 必填 | 描述 |
size | number | 否 | 要读取的字节数。如果不指定,则读取文件中剩余的所有内容。 |
参考
备注
可以读取指定字节数的内容或文件中剩余的全部内容。文件以b模式(如rb)打开时返回ArrayBuffer,否则返回字符串。不带参数读完剩余内容,或某次读取已无数据可读之后,eof()返回true。