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
Is there a way to construct a File from a resource?
Strictly speaking, no. But if you really need a File (to pass to an API), then you can create a temporary file and write the InputStream to that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With