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 values | Description |
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 Functions | Description |
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 Properties | Type | Description |
name |
String | File name including the extension |
path |
String | Path of the file |
fullName |
String | The complete name of the file: path, name, and extension |
baseName |
String | The name of just the file (no path or extension) |
extension |
String | Extension of the file name |
exists |
Boolean | True if the file exists; false if it does not |
readable |
Boolean | True if the file can be read |
writable |
Boolean | True if the file can be written |
executable |
Boolean | True if the file can be executed |
hidden |
Boolean | True if the file is hidden |
eof |
Boolean | True if reading has reached the end of the file |
created |
Date | Time of file creation |
lastModified |
Date | Last time file was modified |
lastRead |
Date | Last time file was read |
size |
Number | Size of file; in bytes |
File Class Methods
File Class Methods | Description | data return |
new File(filename) |
Creates a file object referencing file name; filename = string | no |
open(accessMode) |
Opens the file using the named accessMode | no |
close() |
Closes the file | no |
readByte() |
Reads one byte from the file; Returns a number | yes |
read() |
Returns the complete contents of the file as a string; if can’t read, returns an exception | yes |
readLine() |
Reads one line from file if possible; returns an exception if can’t. Trailing whitespace is retained | yes |
readLines() |
Returns contents of the file as an array of strings--one for each line; Linebreaks are stripped from strings | yes |
writeByte(byte) |
Writes a byte to the file | no |
write(data, length) |
Writes length number of characters from data to the file | no |
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 successful | yes |
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();