Using the file system API
The org.eclipse.core.filesystem plug-in provides a generic API for
interacting with an arbitrary file system. This API is similar to java.io.File,
with a few key differences:
- Plug-ins can install providers for different types of file systems.
- All methods integrate support for reporting progress and responding to cancelation,
making it easier to integrate into a graphical user interface.
- There is support for some additional functionality not available in java.io.File,
such as getting and setting file permissions.
- There are more convenience methods, such as copy, move, and recursive deletion.
In the file system API, the path for any given file is represented as a hierarchical
java.net.URI. The URI scheme represents the kind of file system,
and the URI path component represents the location of the file within the file
system tree. Thus any given hierarchical URI represents a potential file or
directory in some arbitrary file system.
The API for working with files and file systems is found in the
org.eclipse.core.filesystem
)
package. The central API type is
IFileStore
Each instance of IFileStore represents
a single file in the file system. As with IResource, the existence of
an IFileStore instance does not mean that such a file exists on disk. You
can use an IFileStore instance to create, delete, copy, move, or open
streams on files. For a given URI, you can get your hands on an IFileStore
instance using the static method EFS.getStore(URI)
The
IFileSystem
interface can be used to find out things about the file system as a whole. Each
IFileSystem instance represents a single URI scheme, such as "file:",
"ftp:", etc. You can use this type to ask questions such as what file attributes
are supported, or whether the file system is case-sensitive. You can also use
this type to obtain an IFileStore for a given URI.
Most methods on IFileStore have a flag parameter that allows
extra options to be supplied. The flag values can be found
in the
EFS
class. For example, to open an output stream for appending to a file, use:
IFileStore store = ...//some file store
store.openOutputStream(EFS.APPEND, null);
If you want the default behaviour for a method, use EFS.NONE.
The
IFileInfo
interface represents the state of a file at a particular moment in time. In particular,
you can find out if the file exists, whether it is a directory, what its attributes are,
and so on. This information can be modified and then set back into the file. For
example, here is a snippet that sets the read only attribute on a directory:
IFileStore store = ...//some file store
IFileInfo info = store.fetchInfo();
if (info.exists() && info.isDirectory()) {
info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, true);
store.putInfo(info, EFS.SET_ATTRIBUTES, null);
}
This style of API allows you to obtain and change file information with
a single call to the file system. In the above example, there is only
one file system call to fetch the info, and then you can perform as many
operations as you want on the IFileInfo object without hitting
the disk again.
The
EFS
class has static factory methods for obtaining IFileStore and IFileSystem
instances, as well as various option constants and error codes.