Using The API
The
FileSystemManager
interface provides access to Commons VFS. Using this interface
you can locate files and create file systems.
There are a
number of ways
to obtain a
Once you have a
FileSystemManager fsManager = VFS.getManager(); FileObject jarFile = fsManager.resolveFile( "jar:lib/aJarFile.jar" ); Each file is represented by a FileObject instance. Using this interface you can create or delete the file, list its children, read or write its content, and so on. For example: // Locate the Jar file FileSystemManager fsManager = VFS.getManager(); FileObject jarFile = fsManager.resolveFile( "jar:lib/aJarFile.jar" ); // List the children of the Jar file FileObject[] children = jarFile.getChildren(); System.out.println( "Children of " + jarFile.getName().getURI() ); for ( int i = 0; i < children.length; i++ ) { System.out.println( children[ i ].getName().getBaseName() ); } See the FileObject Javadocs for more detail. CacheCommons VFS uses a SoftRefFilesCache to release memory if a file is no longer used by the application. This cache will return the same instance for a file as long as it is "strongly reachable" e.g. you hold a reference to this object. If the FileObject is no longer reachable, and the jvm needs some memory, it will be released. Configuring Commons VFS
Commons VFS is represented using the
FileSystemManager
interface. There are a number of ways to create and configure a
The simplest method is to use the static VFS.getManager() method, which returns the default Commons VFS implementation.
This method will also automatically scan the classpath for a /META-INF/vfs-providers.xml file
(also in jar files).
If such a file is found Commons VFS uses it in addition to the default providers.xml.
This allows you to start using a new filesystem by simply drop its implementation into the classpath.
The configuration file format is described below.
To configure Commons VFS programatically, you can create an
instance of
DefaultFileSystemManager
and configure it manually. The default constructor
Here are the steps for using
You should make sure that you call
The third method for configuring Commons VFS, is to configure
it from a file. Create an instance of
StandardFileSystemManager,
and use its
Configuration File
The configuration file is an XML file. The root element
of the configuration file should be a
The
The
The
The
The
The
The
Below is an example configuration file: <providers> <provider class-name="org.apache.commons.vfs.provider.zip.ZipFileProvider"> <scheme name="zip"/> </provider> <extension-map extension="zip" scheme="zip"/> <mime-type-map mime-type="application/zip" scheme="zip"/> <provider class-name="org.apache.commons.vfs.provider.ftp.FtpFileProvider"> <scheme name="ftp"/> <if-available class-name="org.apache.commons.net.ftp.FTPFile"/> </provider> <default-provider class-name="org.apache.commons.vfs.provider.url.UrlFileProvider"/> </providers> |