File
class provides functionallity for reading and writing
binary and text files. A File
can be instantiated as an object,
giving the scripter complete flexibility when reading and writing files.
In addition, the File
class provides a set of static convenence
functions for reading and writing files in one go.
Typical use of a File
is:
// Reads an entire file in one go var log = File.read('file.log'); // Writes an entire file in one go File.write('copy_of_file.log', log); // Read and write a file line by line var infile = new File('file.log'); infile.open(File.ReadOnly); var outfile = new File('copy_of_file.log'); outfile.open(File.WriteOnly); while (!infile.eof) { var line = infile.readLine(); outfile.write( line ); } infile.close(); outfile.close();
AccessMode
is used in conjunction with File.open
to
specify the mode in which the file is opened.
ReadOnly; Opens the file in read-only mode.
WriteOnly; Opens the file in write-only mode. If this flag is
used with Append,
the file is not truncated; but if used on its own
(or with Truncate),
the file is truncated.
ReadWrite; Opens the file in read/write mode. The file is not truncated.
Append; Opens the file in append mode. (You must actually use
(WriteOnly | Append)
to make the file writable and to go into
append mode.) This mode is very useful when you want to write
something to a log file. The file index is set to the end of the file.
Note that the result is undefined if you position the file index
manually using at() in append mode.
Truncate; Truncates the file.
Translate; Enables carriage returns and linefeed translation for text files under Windows.
exists( fileName : String ) : Boolean; Returns true if fileName
exists; otherwise returns false.
remove( fileName : String ); Deletes the file fileName
if
possible; otherwise throws an exception.
write( fileName : String, content : String ); Writes the string
content to the file fileName
if possible (completely replacing the
original contents if the file already exists); otherwise throws an
exception.
read( fileName : String ) : String; Reads and returns the contents of
the file fileName
if possible; otherwise throws an exception.
isFile( fileName : String ) : Boolean; Returns true if fileName
is a file; otherwise returns false.
isDir( fileName : String ) : Boolean; Returns true if fileName
is a directory; otherwise returns false.
File( fileName : String ); Creates a file object with the with
fileName. If fileName
is missing or is not a String, an exception
is thrown.
name : String; The name of the file including the extension.
path : String; The path of the file.
fullName : String; The fullName of the file, including path, name, and extension.
baseName : String; The name of the file, excluding its path and extension.
extension : String; The file name's extension.
symLink : String; The expansion of the symlink if the file is a symlink; otherwise empty;
exists : Boolean; True if the file exists; otherwise false.
readable : Boolean; True if the file is readable; otherwise false.
writable : Boolean; True if the file is writable; otherwise false.
executable : Boolean; True if the file is executable; otherwise false.
hidden : Boolean; True if the file is hidden; otherwise false.
eof : Boolean; True if reading has reached the end of the file; otherwise false.
created : Date; The creation time of the file.
lastModified : Date; The last modification time of the file.
lastRead : Date; The last time the file was read.
size : Number; The size of the file, in bytes.
open( accessMode : Number ); Opens the file in the mode accessMode if possible; otherwise throws an exception.
close(); Closes the file.
remove((); Deletes the file if possible; otherwise throws an exception.
readByte() : Number; Reads one byte from the file if possible; otherwise throws an exception.
read() : String; Returns the entire content of the file as a string if it can be read; otherwise throws an exception.
readLine() : String; Reads one line from file if possible; otherwise throws an exception. Retains any trailing whitespace.
readLines() : String[]; Returns the contents of the file as an array of Strings, one for each line. Linebreaks are strippped from the strings. If the file could not be read, an exception is thrown.
writeByte( byte : Number ); Writes a byte to the file is possible; otherwise throws an exception.
write( data : String, length : Number ); Writes length
number of
characters from data
to the file if possible; otherwise throws an
exception.
writeLine( data : String ); Writes the line data
to the file and
adds a linebreak. If the file could not be written error
is
returned.