在Java中,我怎样才能从资源构建一个文件? [英] In Java, how can I construct a File from a resource?
问题描述
$ b
getClass()。getResource(...)
返回URL和
getResourceAsStream(...)
返回InputStream。有没有办法从资源构建文件?
你不能。至少不是在资源位于Jar内的标准情况下。 Java文件对象必须指向实际的文件系统对象(文件和文件夹),而资源是Jar文件中的条目。尝试从Jar资源URI构造文件将导致一个异常:
java.lang.IllegalArgumentException
:URI不是分层的
File takes String and URI in its constructors.
getClass().getResource(...)
returns URL and
getResourceAsStream(...)
returns InputStream. Is there a way to construct a File from a resource?
You can't. At least not in the standard case where the resource is inside a Jar. Java File Objects must point to actual file system objects (files and folders), whereas a resource is an entry in a Jar File. Trying to construct a File from a Jar resource URI will result in an exception:
java.lang.IllegalArgumentException
: URI is not hierarchical
这篇关于在Java中,我怎样才能从资源构建一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!