File class

Availability

Flash Media Server 2.

The File class lets applications write to the server's file system. This is useful for storing information without using a database server, creating log files for debugging, or tracking usage. Also, a directory listing is useful for building a content list of streams or shared objects without using Flash Remoting.

The File object allows access to the server file system. To protect against any misuse, Flash Media Server allows access to files within a sandbox specified for the virtual host where the application instance is running.

TIP

 

A sandbox is a security feature that determines how an application can interact with the local file system, the network, or both the local file system and network at the same time. Restricting how a file can interact with the local file system, or the network helps keep your computer and files safe.

The server administrator can set up the sandbox for all applications within a virtual host and can provide additional control to individual applications, if required. The following rules are enforced by the server:

The File object invokes the Application.onStatus event handler to report errors when the following File class methods fail:

Method summary for the File class

Method

Description

File.close()

Closes the file.

File.copyTo()

Copies a file to a different location or to the same location with a different filename.

File.eof()

Returns a Boolean value indicating whether the file pointer is at the end of file (true) or not (false).

File.flush()

Flushes the output buffers of a file.

File.list()

If the file is a directory, returns an array with an element for each file in the directory.

File.mkdir()

Attempts to create a directory in the file directory.

File.open()

Opens a file so that you can read from it or write to it.

File.read()

Reads the specified number of characters from a file and returns a string.

File.readAll()

Reads the file and returns an array with an element for each line of the file.

File.readByte()

Reads the next byte from the file and returns the numeric value of the next byte, or -1 if the operation fails.

File.readln()

Reads the next line from the file and returns it as a string.

File.remove()

Removes the file or directory pointed to by the File object.

File.renameTo()

Moves or renames a file.

File.write()

Writes data to a file.

File.writeAll()

Takes an array as a parameter and calls the File.writeln() method on each element in the array.

File.writeByte()

Writes a byte to a file.

File.writeln()

Writes data to a file and adds a platform-dependent end-of-line character after outputting the last parameter.

getGlobal()

Skips a specified number of bytes and returns the new file position.

File.toString()

Returns a string that indicates the name of the File object.

Property summary for the File class

Property

Description

File.canAppend

Read-only; a Boolean value indicating whether a file was open for appending (true) or not (false).

File.canRead

Read-only; a Boolean value indicating whether a file can be read (true) or not (false).

File.canReplace

Read-only; a Boolean value indicating whether a file was opened with the replace flag enabled (true) or not (false).

File.canWrite

Read-only; a Boolean value indicating whether you can write to a file (true) or not (false).

File.creationTime

Read-only; a Date object containing the time the file was created.

File.exists

Read-only; a Boolean value indicating whether the file or directory exists (true) or not (false).

File.isDirectory

Read-only; a Boolean value indicating whether the file is a directory (true) or not (false).

File.isFile

Read-only; a Boolean value indicating whether the file is a regular data file (true) or not (false).

File.isOpen

Read-only; a Boolean value indicating whether the file has been successfully opened and is still open (true) or not (false).

File.lastModified

Read-only; a Date object containing the time the file was last modified.

File.length

Read-only; for a directory, the number of files in the directory, not counting the current directory and parent directory entries; for a file, the number of bytes in the file.

File.mode

Read-only; the mode of an open file.

File.name

Read-only; a string indicating the name of the file.

File.position

The current offset in the file.

File.type

Read-only; a string specifying the type of data or encoding used when a file is opened.

Constructor for the File class

Availability

Flash Media Server 2.

Usage

fileObject = new File(name)

Parameters

name Specifies the name of the file or directory. The name can contain only UTF-8 encoded characters; high byte values can be encoded using the URI character- encoding scheme. The specified name is mapped to a system path using the mappings specified in the Application.xml file. If the path is invalid, the name property of the object is set to an empty string, and no file operation can be performed.

Returns

A File object if successful; otherwise, null.

Description

Constructor; to create an instance of the File class, use the standard syntax for object creation.

Example

The following code creates an instance of the File class:

var errorLog = new File("/logs/error.txt");