Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a FileObject into a File

I'm using Apache Commons VFS2 (Virtual File System) to monitor change file in directory. org.apache.commons.vfs2.FileListener return org.apache.commons.vfs2.FileObject. How Convert a org.apache.commons.vfs2.FileObject into a java.io.File

like image 561
Gleb Belyaev Avatar asked Aug 28 '13 09:08

Gleb Belyaev


2 Answers

fileobject.getURL().getFile()

should work. The caveat is that we need to convert it first to a Java URL object, which can then be used to resolve the file.

like image 157
laughing_man Avatar answered Nov 10 '22 01:11

laughing_man


You can use

new File(fileobject.getName().getPath());

Note that a VFS file object does not necessarily references a real File, it can also reference a file within a zip file for example. Depends on the resolver you used to obtain a file object.

Additional references:

  1. [VFS-443] Need an easy way to convert from a FileObject to a File - ASF JIRA.
like image 33
GerritCap Avatar answered Nov 09 '22 23:11

GerritCap