Write to a file stream returned from getResourceAsStream()

后端 未结 2 1273
慢半拍i
慢半拍i 2020-12-01 18:08

I am getting a a InputStream from getResourceAsStream(), and I managed to read from the file by passing the returned InputStream to a

2条回答
  •  既然无缘
    2020-12-01 18:58

    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.

提交回复
热议问题