File object

The File objects allow you to work with files. You can read from and write to both text and binary files. Reading is also supported from files stored in your game packages (DCP files). The File object can be created using the following syntax:

var SomeFile = new File("c:\path\filename.txt");


Constructors

File Creates a new file object for a specified filename.

Methods

Operations
SetFilename Sets the filename the File object operates on.
OpenAsText Opens file for text reading or writing.
OpenAsBinary Opens file for binary reading or writing.
Close Closes the file.
SetPosition Sets a new reading/writing point for a currently open file.
Delete Deletes the file accosiated with this File object.
Copy Copies the file to a new location.
Text file access
ReadLine Reads a line from a text file open for reading.
ReadText Reads text from a text file open for reading.
WriteLine Writes a line to a text file open for writing/appending.
WriteText Writes text to a text file open for writing/appending.
Binary file access
ReadBool Reads a boolean value (1 byte) from a binary file open for reading.
ReadByte Reads an 8-bit integer value from a binary file open for reading.
ReadShort Reads a 16-bit integer value from a binary file open for reading.
ReadInt Reads a 32-bit integer value from a binary file open for reading.
ReadFloat Reads a 16-bit floating point value from a binary file open for reading.
ReadDouble Reads a 32-bit floating point value from a binary file open for reading.
ReadString Reads a zero-terminated string value from a binary file open for reading.
WriteBool Writes a boolean value (1 byte) to a binary file open for writing/appending.
WriteByte Writes an 8-bit integer value to a binary file open for writing/appending.
WriteShort Writes a 16-bit integer value to a binary file open for writing/appending.
WriteInt Writes a 32-bit integer value to a binary file open for writing/appending.
WriteFloat Writes a 16-bit floating point value to a binary file open for writing/appending.
WriteDouble Writes a 32-bit floating point value to a binary file open for writing/appending.
WriteString Writes a zero-terminated string value to a binary file open for writing/appending.

Attributes

Type (read only) Returns always "file"
Filename (read only) Returns the filename currently assigned to this object.
Position (read only) Returns current reading/writing position within the file.
Length (read only) Returns length of file.
TextMode (read only) Returns true if the file is open in text mode.
AccessMode (read only) Returns the access mode the file is open in (0 - not open, 1 - reading, 2 - writing, 3 - appending)

File(Filename)

Creates a new file object for a specified filename.

Parameters

Filename
The file this File object should operate on

SetFilename(Filename)

Sets the filename the File object operates on.

Parameters

Filename
The file to be used by this File object

OpenAsText(AccessMode)

Opens file for text reading or writing.

Parameters

AccessMode
Specifies the desired file access, 1 - reading, 2 - writing, 3 - appending

Return value

Returns true is the file was successfuly open.


OpenAsBinary(AccessMode)

Opens file for binary reading or writing.

Parameters

AccessMode
Specifies the desired file access, 1 - reading, 2 - writing, 3 - appending

Return value

Returns true is the file was successfuly open.


Close()

Closes the file.

Remarks

Files are closed automatically when their File objects are destroyed but it's a good idea to close the file explicitly immediately after you're done with working with it.


SetPosition(Position)

Sets a new reading/writing point for a currently open file.

Parameters

Position
The desired reading/writing position

Return value

Returns true if the position has been successfuly set.


Delete()

Deletes the file accosiated with this File object.

Return value

Returns true if the file has been successfuly deleted.


Copy(DestinationFilename, Overwrite)

Copies the file to a new location.

Parameters

DestinationFilename
The new location to copy the file to.
Overwrite
Specifies if the file should overwrite any existing file with the same name (optional, default=true)

Return value

Returns true if the file has been successfuly copied.


ReadLine()

Reads a line from a text file open for reading.

Return value

Returns a string value containing the line or null if file cannot be read / position is at the end of file.


ReadText(Length)

Reads text from a text file open for reading.

Parameters

Length
Number of characters to be read from the file.

Return value

Returns a string value containing the text or null if file cannot be read / position is at the end of file.


WriteLine(Line)

Writes a line to a text file open for writing/appending.

Parameters

Line
The text line to be written to the file

Return value

Returns true if the value has been successfuly written.


WriteText(Text)

Writes text to a text file open for writing/appending.

Parameters

Text
The text to be written to the file

Return value

Returns true if the value has been successfuly written.


ReadBool()

Reads a boolean value (1 byte) from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


ReadByte()

Reads an 8-bit integer value from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


ReadShort()

Reads a 16-bit integer value from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


ReadInt()
ReadLong()

Reads a 32-bit integer value from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


ReadFloat()

Reads a 16-bit floating point value from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


ReadDouble()

Reads a 32-bit floating point value from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


ReadString()

Reads a zero-terminated string value from a binary file open for reading.

Return value

Returns the value or null if file cannot be read / position is at the end of file.


WriteBool(Value)

Writes a boolean value (1 byte) to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.


WriteByte(Value)

Writes an 8-bit integer value to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.


WriteShort(Value)

Writes a 16-bit integer value to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.


WriteInt(Value)
WriteLong(Value)

Writes a 32-bit integer value to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.


WriteFloat(Value)

Writes a 16-bit floating point value to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.


WriteDouble(Value)

Writes a 32-bit floating point value to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.


WriteString(Value)

Writes a zero-terminated string value to a binary file open for writing/appending.

Parameters

Value
The value to be written to the file.

Return value

Returns true if the value has been successfuly written.