Developer Resources

File

Overview

The File class exposes functions which allow access to binary and text files.

Properties

FileMode.Create
A flag specifying that a file should explicitly be created.
FileMode.Open
A flag specifying that a file should explicitly be opened.
FileMode.OpenOrCreate
A flag specifying that a file should be opened if it already exists or created if it doesn't exist.
FileMode.Append
A flag specifying that a file should be opened and appended to.
FileAccess.Read
A flag specifying that a file should be opened only with read rights.
FileAccess.Write
A flag specifying that a file should be opened only with write rights.
FileAccess.ReadWrite
A flag specifying that a file should be opened with both read and write rights.
FileShare.None
A flag specifying that a file can only be accessed by one process at a time.
FileShare.Read
A flag specifying that a file can be read by multiple processes at the same time.
FileShare.Write
A flag specifying that a file can be written to by multiple processes at the same time.
FileShare.ReadWrite
A flag specifying that a file can be read from and written to by multiple processes at the same time.
SeekOrigin.Begin
A flag specifying the seek location at the beginning of the file.
SeekOrigin.Current
A flag specifying the seel location at the current location in the file.
SeekOrigin.End
A flag specifying the seek location at the end of the file.

Methods

File.appendText
Opens a text file for appending.
File.copy
Copies a file.
File.createText
Creates a text file
File.delete
Deletes a file.
File.exists
Determines whether a file exists.
File.getTempFilename
Generates a unique filename for a temporary file.
File.move
Moves a file.
File.open
Opens a file for reading or writing.
File.openText
Opens a text file for reading.

Example

// create a binary file
var f = File.open("data.bin",
                  FileMode.Create,
                  FileAccess.ReadWrite,
                  FileShare.None);

// make a buffer with some data to write       
var buf = new MemoryBuffer(3);
buf[0] = 0;
buf[1] = 1;
buf[2] = 2;

// write the buffer to the file
f.write(buf, 0, 3);

// close the file
f.close();

// check to make sure it exists
if (File.exists("data.bin"))
{
    alert("file created successfully.");
}

// open up the file for reading
f = File.open("data.bin",
              FileMode.Open,
              FileAccess.Read,
              FileShare.None);
buf.clear();

// read 3 bytes from the file
f.read(buf, 0, 3);

alert("The following value should be 2.  It is: " + buf[2]);

File.appendText

static function File.appendText(filename : String) : TextWriter

Arguments

filename
The file to append to.

Returns

Returns a TextWriter object, or null if a problem occurred.

Description

Opens a file specified by the filename parameter for appending. If the file does not already exist, it is created.

File.copy

static function File.copy(source : String, destination) : Boolean

Arguments

source
The file to copy.
destination
The name of the newly copied file.

Returns

Returns true upon success, and false otherwise.

Description

Copies the source file to a new destination file. Returns true if the new file was created and false otherwise.

File.createText

static function File.createText(filename : String) : TextWriter

Arguments

filename
The file to create.

Returns

Returns the method returns a TextWriter object, or null if a problem was encountered.

Description

Creates a text file using the filename specified in the filename parameter. If the file already exists, it will be overwritten.

File.delete

static function File.delete(filename : String) : Boolean

Arguments

filename
The file to delete.

Returns

Returns true upon success, and false otherwise.

Description

Deletes the file specified by filename and returns a boolean value indicating success or failure.

File.exists

static function File.exists(filename : String) : Boolean

Arguments

filename
The path to the file to check for existence.

Returns

Returns true if the file exists, and false otherwise.

Description

Determines whether the file specified by filename exists.

File.getTempFilename

static function File.getTempFilename() : String

Returns

Returns fully-qualified path for a temporary file.

Description

Generates a unique filename for a temporary file. The function will use the system's default temporary path. For Windows systems, the path will be the users configured temporary directory (usually in C:\Documents and Settings\(user name)\Local Settings\Temp). For Linux systems, this will usually be the /tmp directory.

File.move

static function File.move(file_path : String, new_file_path) : Boolean

Arguments

file_path
The file to move.
new_file_path
The path of the newly moved file.

Returns

Returns true upon success, and false otherwise.

Description

Moves the file specified by file_path to the new_file_path and returns a boolean value indicating success or failure.

File.open

static function File.open(filename : String, file_mode : FileMode, file_access : FileAccess, file_share : FileShare) : FileStream

Arguments

filename
The file to open.
file_mode
Specifies the behavior of the open call. Specifies whether the file should be created, opened, or opened and then created if it doesn't already exist. One of FileMode.Create, FileMode.Open or FileMode.OpenOrCreate.
file_access
Specifies the desired file access (read, write). One of FileAccess.Append, FileAccess.Read, FileAccess.Write, or FileAccess.ReadWrite.
file_share
Specifies the file sharing flags. One of FileShare.None, FileShare.Read, FileShare.Write or FileShare.ReadWrite.

Returns

Returns a FileStream object, or null if an error was encountered.

Description

Opens the file specified by filename, applying the functionality requested by the file_mode, file_access and file_share parameters.

File.openText

static function File.openText(filename : String) : TextReader

Arguments

filename
The file to open.

Returns

Returns a TextReader object, or null if an error occurred.

Description

Opens the text file specified by filename and returns a TextReader object.