写入从getResourceAsStream()返回的文件流 [英] Write to a file stream returned from getResourceAsStream()

查看:134
本文介绍了写入从getResourceAsStream()返回的文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 getResourceAsStream()得到了一个 InputStream ,我设法从文件中读取返回 InputStream BufferedReader



我也可以写入文件吗?

解决方案

不是直接的,没有 - getResourceAsStream()是为了在只读资源上返回一个视图。

如果你知道这个资源是一个可写文件,你可以跳过一些箍,例如

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

这应该在unix风格的系统上很好地工作,但是windows文件路径可能会导致消化不良。试试看,但是,你可能会好的。


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?

解决方案

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.

这篇关于写入从getResourceAsStream()返回的文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆