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
            Asked
            
        
        
            Active
            
        
            Viewed 1.2k times
        
    11
            
            
        
        Gleb Belyaev
        
- 1,009
 - 1
 - 14
 - 23
 
- 
                    this is just an interface. what implementation are you using? – Philipp Sander Aug 28 '13 at 09:34
 - 
                    If you mean FileListener, there are all methods return FileObject – Gleb Belyaev Aug 28 '13 at 09:37
 - 
                    no. FileObject is an interface. try to figure out what concrete implementation is returned. – Philipp Sander Aug 28 '13 at 09:40
 - 
                    Even if it's an interface, the return type for the functions can be used to get to the destination. – laughing_man Nov 04 '13 at 21:26
 
3 Answers
5
            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.
        laughing_man
        
- 3,756
 - 1
 - 20
 - 20
 
1
            
            
        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:
        Sergey Vyacheslavovich Brunov
        
- 17,291
 - 7
 - 48
 - 81
 
        GerritCap
        
- 1,606
 - 10
 - 9
 
- 
                    1i don't think this will work, if the FileObject represents a File in a zip. – Philipp Sander Aug 28 '13 at 09:48
 - 
                    Especially on Windows, since it only return path on volume, and not path on machine. – Riduidel May 28 '14 at 13:09
 
0
            
            
        org.apache.commons.vfs2.FileObjectis an interface. 
My guess is, that you are working with the following implementation org.apache.commons.vfs2.provider.local.LocalFile
This class offers the method getLocalFile()which return an object of the class java.io.File
If you are not dealing with a LocalFile most of the other implementations offer doGetInputStream() which can be used to create a File object
        Philipp Sander
        
- 10,139
 - 6
 - 45
 - 78
 
- 
                    1the `getLocalFile()` is unfortunatly protected (as the javadoc indicates) which makes it unusable. – Riduidel May 28 '14 at 13:10
 - 
                    1