How to convert InputStream to FileInputStream

Frank :

I have this line in my program :

InputStream Resource_InputStream=this.getClass().getClassLoader().getResourceAsStream("Resource_Name");

But how can I get FileInputStream from it [Resource_InputStream] ?

BalusC :

Use ClassLoader#getResource() instead if its URI represents a valid local disk file system path.

URL resource = classLoader.getResource("resource.ext");
File file = new File(resource.toURI());
FileInputStream input = new FileInputStream(file);
// ...

If it doesn't (e.g. JAR), then your best bet is to copy it into a temporary file.

Path temp = Files.createTempFile("resource-", ".ext");
Files.copy(classLoader.getResourceAsStream("resource.ext"), temp, StandardCopyOption.REPLACE_EXISTING);
FileInputStream input = new FileInputStream(temp.toFile());
// ...

That said, I really don't see any benefit of doing so, or it must be required by a poor helper class/method which requires FileInputStream instead of InputStream. If you can, just fix the API to ask for an InputStream instead. If it's a 3rd party one, by all means report it as a bug. I'd in this specific case also put question marks around the remainder of that API.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert OutputStream to InputStream?

How do I convert a String to an InputStream in Java?

How to convert a Reader to InputStream and a Writer to OutputStream?

How to convert InputStream to virtual File

How to convert InputStream into Source?

what is the difference in using InputStream instead of FileInputStream while creating the FileInputStream object

How to convert FileInputStream to InputStream?

How can I convert an Object to Inputstream

How do I convert an InputStream to a String in Java?

How to convert BufferedImage to InputStream?

How to convert StringBuffer to InputStream in Java ME?

How to convert FileInputStream into string in java?

How to convert an InputStream to a DataHandler?

How can I convert an uncompressed InputStream into a gzip'ed InputStream efficiently?

Convert InputStream into FileInputStream

How can I convert ZipInputStream to InputStream?

How to convert InputStream to int

How to convert ArchiveEntry to InputStream?

How to convert the response from Okhttp into inputstream

How to convert Source[ByteString, Any] to InputStream

How to convert Reactor Flux<String> to InputStream

How to convert a String to an InputStream in Kotlin?

How to convert Reader to InputStream in java

How to convert FileInputStream to S3ObjectInputStream

How to accommodate a FileInputStream object for InputStream object in the constructor call to the class while initializing?

How to convert an InputStream object to a File object?

How to convert a PDF InputStream to an Html String?

How to convert InputStream to Json in Camel K

Does FileInputStream incorrectly implement InputStream?