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

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

问题描述

我从 getResourceAsStream() 获取了一个 InputStream,我设法通过将返回的 InputStream 传递给一个 InputStream 来从文件中读取代码>BufferedReader.

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?

推荐答案

不是直接的,不是 - 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);

这在 Unix 风格的系统上应该可以很好地工作,但是 Windows 文件路径可能会导致这种消化不良.试试看吧,不过,你可能会没事的.

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天全站免登陆