The FileEntity class provides low-level file read and write operations.
It allows scripts and HTTP servers to open, read, write, seek, and close files efficiently.
FileEntity can also be used as a response body for HttpResponse.raw() to serve static or downloadable files.
FileEntity enables direct file operations, including:
Data objects)path: stringThe full path of the file represented by this FileEntity instance.
Example:
seek(offset: number): booleanMoves the file pointer to the specified byte offset.
Returns true if successful, or false if seeking fails.
Parameters:
| Name | Type | Description |
|---|---|---|
offset |
number |
The byte offset to move to. |
Example:
read(size: number): DataReads a specified number of bytes from the current file position and returns them as a Data object.
Parameters:
| Name | Type | Description |
|---|---|---|
size |
number |
Number of bytes to read. |
Returns:
A Data object containing the read bytes.
Example:
write(data: Data): voidWrites the provided Data to the current file position.
Throws an error if the file was not opened in a writable mode.
Parameters:
| Name | Type | Description |
|---|---|---|
data |
Data |
The binary data to write to the file. |
Example:
close(): voidCloses the file and releases the associated resources.
After closing, you should not call read() or write() again.
Example:
static openForReading(path: string): FileEntityOpens a file in read-only mode. Throws an error if the file does not exist or cannot be read.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string |
The file path to open. |
Example:
static openNewForWriting(path: string): FileEntityOpens a file in write-only mode. If the file already exists, it will be overwritten.
Example:
static openForWritingAndReading(path: string): FileEntityOpens a file in read/write mode. If the file does not exist, it will be created automatically.
Example:
static openForMode(path: string, mode: string): FileEntityOpens a file using the specified access mode.
The supported modes follow standard POSIX file semantics, but it is strongly recommended to use binary-safe modes such as "rb" or "r+b" for better cross-platform compatibility, since FileEntity performs all read/write operations in binary mode internally.
Parameters:
| Mode | Description |
|---|---|
"r" / "rb" |
Read-only mode (file must exist). "rb" is recommended for better compatibility. |
"w" / "wb" |
Write-only mode (creates a new file or overwrites the existing one). "wb" is recommended. |
"a" / "ab" |
Append mode (writes data to the end of the file; creates the file if missing). "ab" is recommended. |
"r+" / "r+b" |
Read/write mode (file must exist). "r+b" is recommended for binary read/write. |
"w+" / "w+b" |
Read/write mode (creates or overwrites the file). "w+b" is recommended. |
"a+" / "a+b" |
Read/append mode (creates the file if missing). "a+b" is recommended. |
💡 Note: Always prefer binary-safe modes (those ending with
b), such as"rb","wb", or"r+b". This ensures consistent behavior across platforms and avoids newline or encoding conversion issues, sinceFileEntityhandles all file I/O as binary streams.
Example:
FileEntity can be used directly as the body of an HTTP response via HttpResponse.raw().
This allows serving files or implementing file download endpoints.
Example:
Clients accessing /download will directly receive the file as a download.
| Method | Description | Typical Use Case |
|---|---|---|
seek() |
Moves the read/write position | Random access or partial reads |
read() |
Reads data from file | Reading text or binary content |
write() |
Writes data to file | Saving logs, exporting data |
close() |
Closes the file | Always call after use |
openForReading() |
Opens a file for reading | Serving static content |
openNewForWriting() |
Opens a file for writing (overwrite) | Creating new files |
openForWritingAndReading() |
Opens for both reading and writing | Editing or streaming |
openForMode() |
Opens file in custom mode | POSIX-compatible access control |