public interface FileOrURLAccessor extends Settable
This interface is necessary because Java does not support multiple
inheritance and we have two classes (FileParameter and FilePortParameter)
that have a common interface, but do not have an immediate parent class.
In addition,
FileAttribute
shares this interface.
This interface is implemented by an Attribute or Parameter that specifies a file or URL. The value of this Attribute or Parameter, accessed by getExpression(), is a string that names a file or URL. If the model containing this attribute has been saved to a MoML file, then the file name can be given relative to the directory containing that MoML file. If the model has not been saved to a file, then the classpath is used for identifying relative file names.
Files can be given relative to a base, where the base is the URI of the first container above this one that has a URIAttribute. Normally, this URI specifies the file or URL containing the model definition. Thus, files that are referred to here can be kept in the same directory as the model, or in a related directory, and can moved together with the model.
The following special file names are understood:
A file name can also contain the following strings that start with "$", which get substituted with the appropriate values
String | Description | Property |
---|---|---|
$CWD |
The current working directory | user.dir |
$HOME |
The user's home directory | user.home |
$PTII |
The home directory of the Ptolemy II installation | ptolemy.ptII.dir |
$TMPDIR |
The temporary directory | java.io.tmpdir |
If a file name begins with the string "$CLASSPATH", followed by either "/" or "\", then when the file is opened for reading, the openForReading() method will search for the file relative to the classpath (using the getResource() method of the current class loader). This will only work for a file that exists, and thus the openForWriting() method will not understand the "$CLASSPATH" string; this makes sense since the classpath typically has several directories, and it would not be obvious where to create the file. The asURL() method also recognizes the "$CLASSPATH" string, but not the asFile() method (which is typically used when accessing a file for writing).
URIAttribute
Red (cxh) |
Red (cxh) |
Settable.Visibility
EXPERT, FULL, NONE, NOT_EDITABLE
Modifier and Type | Method and Description |
---|---|
java.io.File |
asFile()
Return the file as a File object.
|
java.net.URL |
asURL()
Return the file as a URL.
|
void |
close()
Close the file.
|
java.net.URI |
getBaseDirectory()
Return the directory to use as the base for relative file or URL names.
|
java.io.BufferedReader |
openForReading()
Open the file or URL for reading.
|
java.io.Writer |
openForWriting()
Open the file for writing.
|
java.io.Writer |
openForWriting(boolean append)
Open the file for writing or appending.
|
void |
setBaseDirectory(java.net.URI directory)
Set the directory to use as the base for relative file or URL names.
|
addValueListener, getDefaultExpression, getDisplayName, getExpression, getValueAsString, getVisibility, removeValueListener, setExpression, setVisibility, validate
description, getContainer, getFullName, getName, getName, setName
java.io.File asFile() throws IllegalActionException
The file need not exist for this method to succeed. Thus, this method can be used to determine whether a file with a given name exists, prior to calling openForWriting(). A typical usage looks like this:
FileAttribute fileAttribute; ... File file = fileAttribute.asFile(); if (file.exists()) { ... Ask the user if it's OK to overwrite... ... Throw an exception if not... } // The following will overwrite an existing file. Writer writer = new PrintWriter(fileAttribute.openForWriting());
IllegalActionException
- If a parse error occurs
reading the file name.getBaseDirectory()
java.net.URL asURL() throws IllegalActionException
IllegalActionException
- If the file cannot be read, or
if the file cannot be represented as a URL (e.g. System.in).void close() throws IllegalActionException
IllegalActionException
- If the file or URL cannot be
closed.java.net.URI getBaseDirectory()
setBaseDirectory(URI)
,
URIAttribute.getModelURI(NamedObj)
java.io.BufferedReader openForReading() throws IllegalActionException
IllegalActionException
- If the file or URL cannot be
opened.getBaseDirectory()
java.io.Writer openForWriting() throws IllegalActionException
IllegalActionException
- If the file cannot be opened
or created.getBaseDirectory()
,
asFile()
java.io.Writer openForWriting(boolean append) throws IllegalActionException
append
- If true, then append to the file rather than
overwriting.IllegalActionException
- If the file cannot be opened
or created.getBaseDirectory()
,
asFile()
void setBaseDirectory(java.net.URI directory)
directory
- The base directory.getBaseDirectory()
,
URIAttribute.getModelURI(NamedObj)