Convert a FileObject into a File

2019-06-16 02:21发布

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

3条回答
别忘想泡老子
2楼-- · 2019-06-16 02:26

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.
查看更多
爷、活的狠高调
3楼-- · 2019-06-16 02:28
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.

查看更多
成全新的幸福
4楼-- · 2019-06-16 02:37

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

查看更多
登录 后发表回答