File

Moduleejs.io
Namespace"ejs.io"
Definition class File
SpecifiedEjs-11.
InheritanceFile inherit Object

The File class provides a foundation of I/O services to interact with physical files.

Each File object represents a single file, a named path to data stored in non-volatile memory. A File object provides methods for creating, opening, reading, writing and deleting a file, and for accessing and modifying information about the file.


Properties

QualifiersPropertyTypeDescription
public const AppendNumberFile access mode representing a file opened for append write access.
public const ClosedNumberFile access mode representing a Closed file.
public const CreateNumberFile access mode where the file will be re-created when opened.
public const OpenNumberFile access mode representing an Opened file.
public const ReadNumberFile access mode representing an opened file for read access.
public const TruncateNumberFile access mode where the file will be truncated when opened.
public const WriteNumberFile access mode representing an opened file for write access.

File Methods

QualifiersMethod
public get absolutePath(): String
 Return an absolute path name for the file.
public get basename(): String
 Get the base name of a file.
public close(graceful: Boolean): Void
 Close the input stream and free up all associated resources.
public copy(toPath: String): Void
 Copy a file.
public get created(): Date
 Get when the file was created.
public static createTempFile(directory: String): File
public get dirname(): String
 Get the directory name portion of a file.
public get exists(): Boolean
 Test to see if this file exists.
public get extension(): String
 Get the file extension portion of the file name.
File(path: String)
 Create a new File object and set the file object's path.
public flush(): Void
 Flush the stream and the underlying file data.
public freeSpace(path: String): Number
 Return the free space in the file system.
iterator override get(deep: Boolean): Iterator
 Get an iterator for this file to be used by "for (v in files)".
public static getBytes(path: String): ByteArray
 Get the file contents as an array of lines.
public getFiles(enumDirs: Boolean): Array
 Get a list of files in a directory.
public static getLines(path: String, encoding: String): Array
 Get the file contents as an array of lines.
public static getString(path: String, encoding: String): String
 Get the file contents as a string.
iterator override getValues(deep: Boolean): Iterator
 Get an iterator for this file to be used by "for each (v in obj)".
public static getXML(path: String): XML
 Get the file contents as an XML object.
public get hasDriveSpec(): Boolean
 Determine if the file path has a drive spec (C:) in it's name.
public get isDir(): Boolean
 Determine if the file name is a directory.
public get isOpen(): Boolean
 Determine if the file is currently open for reading or writing.
public get isRegular(): Boolean
 Determine if the file name is a regular file.
public get lastAccess(): Date
 Get when the file was last accessed.
public get length(): Number
 Get the length of the file associated with this File object.
public makeDir(permissions: Number): Void
 Make a new directory.
public get mode(): Number
 Get the file access mode.
public get modified(): Date
 Get when the file was created or last modified.
public get name(): String
 Get the name of the file associated with this File object.
public get newline(): String
 Return the new line characters.
public set newline(terminator: String): Void
 Set the new line characters.
public open(mode: Number, permissions: Number): Void
 Open a file using the current file name.
public static openBinaryStream(filename: String, mode: Number, permissions: Number): BinaryStream
 Open a file and return a BinaryStream object.
public static openFileStream(filename: String, mode: Number, permissions: Number): File
 Open a file and return a Stream object.
public static openTextStream(filename: String, mode: Number, permissions: Number): TextStream
 Open a file and return a TextStream object.
public get parent(): String
 Get the parent directory of the absolute path of the file.
public get pathDelimiter(): String
 Return the path segment delimiter.
public set pathDelimiter(delim: String): Void
 Set the path segment delimiter.
public get permissions(): Number
 Get the file security permissions.
public set permissions(mask: Number): Void
 Set the file security permissions.
public get position(): Number
 Get the current I/O position in the file.
public set position(value: Number): Void
 Seek to a new location in the file and set the File marker to a new read/write position.
public static put(path: String, permissions: Number, args: Array): Void
 Put the file contents.
public read(buffer: ByteArray, offset: Number, count: Number): Number
 Read a block of data from a file into a byte array.
public readBytes(count: Number): ByteArray
 Read data bytes from a file and return a byte array containing the data.
public get relativePath()
 Return a relative path name for the file.
public remove(): Void
 Delete the file associated with the File object.
public removeDir(recursive: Boolean): Void
 Removes a directory.
public rename(toFile: String): Void
 Rename a file.
public setCallback(callback: Function): Void
 Put the file stream into async mode and define a completion callback.
public get unixPath(): String
 Return an absolute unix style path name for the file.
public write(items: Array): Number
 Write data to the file.

Method Detail

get public absolutePath(): String

Return an absolute path name for the file.

Returns
A string containing an absolute file name relative to the file system's root directory. The file name is canonicalized such that multiple directory separators and ".." segments are removed.
See Also
relativePath

get public basename(): String

Get the base name of a file.

Description
Returns the base name portion of a file name. The base name portion is the trailing portion without any directory elements.
Returns
A string containing the base name portion of the file name.

public close(graceful: Boolean): Void

Close the input stream and free up all associated resources.

Parameters
graceful: Boolean if true, then close the file gracefully after writing all pending data. [default: true]

public copy(toPath: String): Void

Copy a file.

Description
If the destination file already exists, the old copy will be overwritten as part of the copy operation.
Parameters
toPath: String New destination file path name.

Throws

IOError: if the copy is not successful.


get public created(): Date

Get when the file was created.

Returns
A date object with the date and time or null if the method fails.

static public createTempFile(directory: String): File

get public dirname(): String

Get the directory name portion of a file.

Description
The dirname name portion is the leading portion including all directory elements and excluding the base name. On some systems, it will include a drive specifier.
Returns
A string containing the directory name portion of the file name.

get public exists(): Boolean

Test to see if this file exists.

Returns
True if the file exists.

get public extension(): String

Get the file extension portion of the file name.

Returns
String containing the file extension.

File(path: String)

Create a new File object and set the file object's path.

Parameters
path: String the name of the file to associate with this file object.

public flush(): Void

Flush the stream and the underlying file data.

Description
Will block while flushing. Note: may complete before the data is actually written to disk.

public freeSpace(path: String): Number

Return the free space in the file system.

Parameters
path: String
Returns
The number of 1M blocks (1024 * 1024 bytes) of free space in the file system.

override iterator get(deep: Boolean): Iterator

Get an iterator for this file to be used by "for (v in files)".

Description
Return the seek positions for each byte.
Parameters
deep: Boolean Follow the prototype chain. Only implemented in ECMA compliance mode.. [default: false]
Returns
An iterator object.
Specified
Ejs-11.

static public getBytes(path: String): ByteArray

Get the file contents as an array of lines.

Description
Each line is a string. This is a static method that opens the file, reads the contents and closes the file.
Parameters
path: String the name of the file to read.
Returns
An array of strings.

Throws

IOError: if the file cannot be read


public getFiles(enumDirs: Boolean): Array

Get a list of files in a directory.

Description
The returned array contains the base file name portion only.
Parameters
enumDirs: Boolean If set to true, then dirList will include sub-directories in the returned list of files. [default: false]
Returns
An Array of strings containing the filenames in the directory.

static public getLines(path: String, encoding: String): Array

Get the file contents as an array of lines.

Description
Each line is a string. This is a static method that opens the file, reads the contents and closes the file.
Parameters
path: String the name of the file to read.
Returns
An array of strings.

Throws

IOError: if the file cannot be read


static public getString(path: String, encoding: String): String

Get the file contents as a string.

Description
This is a static method that opens the file, reads the contents and closes the file.
Parameters
path: String the name of the file to read.
Returns
A string.

Throws

IOError: if the file cannot be read


override iterator getValues(deep: Boolean): Iterator

Get an iterator for this file to be used by "for each (v in obj)".

Description
Return each byte of the file in turn.
Parameters
deep: Boolean Follow the prototype chain. Only implemented in ECMA compliance mode.. [default: false]
Returns
An iterator object.
Specified
Ejs-11.

static public getXML(path: String): XML

Get the file contents as an XML object.

Description
This is a static method that opens the file, reads the contents and closes the file.
Parameters
path: String the name of the file to read.
Returns
An XML object.

Throws

IOError: if the file cannot be read


get public hasDriveSpec(): Boolean

Determine if the file path has a drive spec (C:) in it's name.

Description
Only relevant on Windows like systems.
Returns
True if the file path has a drive spec.

get public isDir(): Boolean

Determine if the file name is a directory.

Returns
True if the file is a directory.

get public isOpen(): Boolean

Determine if the file is currently open for reading or writing.

Returns
True if the file is currently open via.

get public isRegular(): Boolean

Determine if the file name is a regular file.

Returns
True if the file is a regular file and not a directory.

get public lastAccess(): Date

Get when the file was last accessed.

Returns
A date object with the date and time or null if the method fails.

get public length(): Number

Get the length of the file associated with this File object.

Returns
The number of bytes in the file or -1 if length determination failed.

public makeDir(permissions: Number): Void

Make a new directory.

Description
Makes a new directory and all required intervening directories. If the directory already exists, the function returns without throwing an exception.
Parameters

Throws

IOError: if the directory cannot be created.


get public mode(): Number

Get the file access mode.

Returns
File access mode with values ored from: Read, Write, Append, Create, Open, Truncate.

get public modified(): Date

Get when the file was created or last modified.

Returns
A date object with the date and time or null if the method fails.

get public name(): String

Get the name of the file associated with this File object.

Returns
The name of the file or null if there is no associated file.

get public newline(): String

Return the new line characters.

Returns
The new line delimiting characters Usually "\n" or "\r\n".

set public newline(terminator: String): Void

Set the new line characters.

Parameters
terminator: String the new line termination characters Usually "\n" or "\r\n"

public open(mode: Number, permissions: Number): Void

Open a file using the current file name.

Description
This method requires a file instance.
Parameters
mode: Number optional file access mode with values ored from: Read, Write, Append, Create, Open, Truncate. Defaults to Read. [default: Read]
permissions: Number optional permissions. Defaults to the App.permissions.

Throws

IOError: if the path or file cannot be created.


static public openBinaryStream(filename: String, mode: Number, permissions: Number): BinaryStream

Open a file and return a BinaryStream object.

Description
This is a static method.
Parameters
filename: String The name of the file to create.
mode: Number optional file access mode with values ored from: Read, Write, Append, Create, Open, Truncate. Defaults to Read. [default: Read]
permissions: Number optional permissions. Defaults to the App.permissions.
Returns
A BinaryStream object which implements the Stream interface.

Throws

IOError: if the path or file cannot be created.


static public openFileStream(filename: String, mode: Number, permissions: Number): File

Open a file and return a Stream object.

Description
This is a static method.
Parameters
filename: String The name of the file to create.
mode: Number optional file access mode with values ored from: Read, Write, Append, Create, Open, Truncate. Defaults to Read. [default: Read]
permissions: Number optional permissions. Defaults to the App.permissions.
Returns
A File object which implements the Stream interface.

Throws

IOError: if the path or file cannot be created.


static public openTextStream(filename: String, mode: Number, permissions: Number): TextStream

Open a file and return a TextStream object.

Description
This is a static method.
Parameters
filename: String The name of the file to create.
mode: Number optional file access mode with values ored from: Read, Write, Append, Create, Open, Truncate. Defaults to Read. [default: Read]
permissions: Number optional permissions. Defaults to the App.permissions.
Returns
A TextStream object which implements the Stream interface.

Throws

IOError: if the path or file cannot be created.


get public parent(): String

Get the parent directory of the absolute path of the file.

Returns
The parent directory.

get public pathDelimiter(): String

Return the path segment delimiter.

Returns
The path segment delimiter. Usually "/" or "\\".

set public pathDelimiter(delim: String): Void

Set the path segment delimiter.

Parameters
delim: String the new path segment delimiter. Usually "/" or "\\"

get public permissions(): Number

Get the file security permissions.

Returns
The file permission mask. This is a POSIX file creation mask.

set public permissions(mask: Number): Void

Set the file security permissions.

Returns
The file permission mask. This is a POSIX file creation mask.

get public position(): Number

Get the current I/O position in the file.

Returns
The current read / write position in the file.

Throws

IOError: if the seek failed.


set public position(value: Number): Void

Seek to a new location in the file and set the File marker to a new read/write position.

Parameters

Throws

IOError: if the seek failed.


static public put(path: String, permissions: Number, args: Array): Void

Put the file contents.

Description
This is a static method that opens the file, writes the contents and closes the file.
Parameters
path: String the name of the file to write.
permissions: Number optional permissions. Defaults to the App.permissions.
Returns
An XML object.

Throws

IOError: if the file cannot be written


public read(buffer: ByteArray, offset: Number, count: Number): Number

Read a block of data from a file into a byte array.

Description
This will advance the read position.
Parameters
buffer: ByteArray Destination byte array for the read data.
offset: Number Offset in the byte array to place the data.
count: Number Number of bytes to read.
Returns
A count of the bytes actually read.

Throws

IOError: if the file could not be read.


public readBytes(count: Number): ByteArray

Read data bytes from a file and return a byte array containing the data.

Parameters
count: Number Number of bytes to read.
Returns
A byte array containing the read data.

Throws

IOError: if the file could not be read.


get public relativePath()

Return a relative path name for the file.

Returns
A string containing a file path name relative to the application's current working directory.

public remove(): Void

Delete the file associated with the File object.

Description
If the file is opened, it is first closed.

Throws

IOError: if the file exists and could not be deleted.


public removeDir(recursive: Boolean): Void

Removes a directory.

Parameters
recursive: Boolean [default: false]

Throws

IOError: if the directory exists and cannot be removed.


public rename(toFile: String): Void

Rename a file.

Description
If the new file name exists it is removed before the rename.
Parameters

Throws

IOError: if the original file does not exist or cannot be renamed.


public setCallback(callback: Function): Void

Put the file stream into async mode and define a completion callback.

Description
When in async mode, I/O will be non-blocking and the callback function will be invoked when the there is read data available or when the write side can accept more data.
Parameters
callback: Function Callback function to invoke when the pending I/O is complete. The callback is invoked with the signature: function callback(e: Event): void. Where e.data == stream.

get public unixPath(): String

Return an absolute unix style path name for the file.

Description
Used on Windows when using cygwin to return a cygwin styled path without drive specifiers.
Returns
A string containing an absolute file name relative to the cygwin file system's root directory. The file name is canonicalized such that drive specifiers, multiple directory separators and ".." segments are removed.
See Also
absolutePath,, relativePath

public write(items: Array): Number

Write data to the file.

Description
If the stream is in sync mode, the write call blocks until the underlying stream or endpoint absorbes all the data. If in async-mode, the call accepts whatever data can be accepted immediately and returns a count of the elements that have been written.
Parameters
items: Array The data argument can be ByteArrays, strings or Numbers. All other types will call serialize first before writing. Note that numbers will not be written in a cross platform manner. If that is required, use the BinaryStream class to write Numbers.
Returns
The number of bytes written.

Throws

IOError: if the file could not be written.


Embedthis Software LLC, 2003-2009. All rights reserved. Embedthis is a trademark of Embedthis Software LLC.