Built-in Functions
Structures
Built-in Variables and Constants
Conventions
Exchanges
File.read
Read data from a file.
read()
read(size)Examples
Read file content in chunks.
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()
}Returns
| Type | Description |
string / ArrayBuffer | Returns the content read. At the end of file it returns an empty string |
Arguments
| Name | Type | Required | Description |
size | number | No | Number of bytes to read. If not specified, reads all remaining content in the file. |
See Also
Remarks
Can read a specified number of bytes or all remaining content in the file. Returns an ArrayBuffer when the file was opened in b mode (e.g. rb), otherwise a string. After reading the rest without a size argument, or after a read that finds no more data, eof() returns true.