Write to a file stream returned from getResourceAsStream()

Not directly, no – getResourceAsStream() is intended to return a view on read-only resources.

If you know that the resource is a writeable file, though, you can jump through some hoops, e.g.

URL resourceUrl = getClass().getResource(path);
File file = new File(resourceUrl.toURI());
OutputStream output = new FileOutputStream(file);

This should work nicely on unix-style systems, but windows file paths might give this indigestion. Try it and find out, though, you might be OK.

Leave a Comment