在Java中,我怎样才能从资源构建一个文件? [英] In Java, how can I construct a File from a resource?

查看:148
本文介绍了在Java中,我怎样才能从资源构建一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件在其构造函数中使用String和URI。


$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆