File Class

The File class is useful for reading and writing external text files.

AccessMode Enumerator

The File Class contains an enumerator that works with file.open to set the mode of a file when it is opened. These items are the argument for an “open” command, and are preceeded with the word “File”. The values are shown:

AccessMode valuesDescription

ReadOnly

Opens the file as read-only

WriteOnly

Opens the file as write-only. If used with Append, the file is not truncated. If used on its own (or with Truncate), the file is truncated.

ReadWrite

Opens the file in read/write mode. Note that the file is not truncated.

Append

Opens the file in append mode. (WriteOnly must be used with it to make a file appendable: WriteOnly | Append) Useful for writing to a log file.

Truncate

Truncates the file

Translate

Enables carriage returns and line-feed translation for text files in Windows

To open a file object called “f” as write only, issue this command:

f.open(File.WriteOnly | File.Translate);

The Translate flag is needed for Windows files.

Static Functions

Unlike methods, static functions do not reference a specific object. To use any of these commands, precede it with “File.” This indicates that these functions are linked to the File class.

Static FunctionsDescription

exists(filename)

Boolean: true if fileName exists; false if it does not (filename = string)

remove(filename)

If filename exists, will delete. If not, returns an exception (filename = string)

write(filename, content)

Writes content to the filename. This completely replaces original contents if the file already exists. Returns an exception if can’t write to file.

filename = string; content = string

read(filename)

Reads and returns the contents of the file, if possible. If not, returns an exception.

filename = string

isFile(filename)

Boolean: true if filename is a file; false if it is not

isDir(filename)

Boolean: true if filename is a directory; false if it is not

For example, to check if a file called “logfile.txt” exists, issue the command:

File.exists(“logfile.txt”);

File Class Properties

All properties in the File class are read only.

File Class PropertiesTypeDescription

name

StringFile name including the extension

path

StringPath of the file

fullName

StringThe complete name of the file: path, name, and extension

baseName

StringThe name of just the file (no path or extension)

extension

StringExtension of the file name

exists

BooleanTrue if the file exists; false if it does not

readable

BooleanTrue if the file can be read

writable

BooleanTrue if the file can be written

executable

BooleanTrue if the file can be executed

hidden

BooleanTrue if the file is hidden

eof

BooleanTrue if reading has reached the end of the file

created

DateTime of file creation

lastModified

DateLast time file was modified

lastRead

DateLast time file was read

size

NumberSize of file; in bytes

File Class Methods

File Class MethodsDescriptiondata return

new File(filename)

Creates a file object referencing file name; filename = stringno

open(accessMode)

Opens the file using the named accessModeno

close()

Closes the fileno

readByte()

Reads one byte from the file; Returns a numberyes

read()

Returns the complete contents of the file as a string; if can’t read, returns an exceptionyes

readLine()

Reads one line from file if possible; returns an exception if can’t. Trailing whitespace is retainedyes

readLines()

Returns contents of the file as an array of strings--one for each line; Linebreaks are stripped from stringsyes

writeByte(byte)

Writes a byte to the fileno

write(data, length)

Writes length number of characters from data to the fileno

writeLine(data)

Writes the line “data” to the file, and adds a linebreak.no

copyTo(newName)

copies the current file to "newname" and returns true if the copy was successfulyes

An example of using these commands is given:

f = new File(a.path + "/pressure.txt");

f.open(File.WriteOnly | File.Translate);

f.writeLine("Velocity, Pressure");

f.close();

A step-by-step explanation of this follows:

The following creates a file object called f, and assigns the name “pressure.txt”.

f = new File("pressure.txt");

The file is then opened, and two AccessMode enumerators are used to indicate that this file is write-only, and that it is used in Windows (the Translate modifier):

f.open(File.WriteOnly | File.Translate);

The words “Velocity” and “Pressure” are written to the file to be used as column headers:

f.writeLine("Velocity, Pressure");

The file is closed:

f.close();