Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

java - Write to a file stream returned from getResourceAsStream()

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

Is there any way I can write to the file as well?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...