Commons VFS

Utilizzo di Apache Commons VFS

Dipendenza nel pom.xml

<dependency>
   <groupId>commons-vfs</groupId>
   <artifactId>commons-vfs</artifactId>
   <version>1.0</version>
</dependency>

Import:

import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;

URI e' una stringa:

    protected String fileUri;

Oltre ai file sul filesystem, VFS permette di accedere facilmente a risorse tramite

Http:
http://myusername@somehost/index.html

Ftp, Sftp, ssh etc:
sftp://myusername:mypassword@somehost/pub/downloads/somefile.tgz

File temporaneo:
tmp://dir/somefile.txt

Usando class loader:
res:path/in/classpath/image.png

In ram:
ram:///any/path/to/file.txt

Ottenere un FileObject:

    vfsManager = VFS.getManager();
    FileObject dataFile = vfsManager.resolveFile(fileUri);