java中的FileWriter和InputStream

发布于 2024-09-09 11:02:00 字数 522 浏览 5 评论 0原文

之前,当我在 fileWriter 中使用 file 时,它​​可以工作,但是现在,由于我使用 getResourceAsStream 而不是 file,我怎样才能使它工作?

FileWriter fstream = new FileWriter(new File("filename"), true);

现在,当我通过时,

InputStream is = getResourceAsStream("filename");
FileWriter fstream = new FileWriter(is, true);

我必须更改它,因为当我使用 Maven 程序集插件创建一个可运行的 jar 时,jar 中没有 src/main/resources

编辑:

感谢 casablanca 和其他人的指出指出我的错误,跟进:

我需要写入一个文件,但附加到它,并保留原始内容。当然,我需要知道我正在写入哪个文件。

Before, when I used file in my fileWriter it worked, but now, since I'm using getResourceAsStream instead of file, how can I make it work?

FileWriter fstream = new FileWriter(new File("filename"), true);

Now, when I pass

InputStream is = getResourceAsStream("filename");
FileWriter fstream = new FileWriter(is, true);

I had to change it, because when I create a runnable jar with the maven assembly plugin there is no src/main/resources in the jar

EDIT:

thanks to casablanca and others for pointing out my mistakes, follow up :

I need to write to a file, but append to it, with preserving the original content. And I need to know to which file I'm writing of course.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-09-16 11:02:00

InputStream 表示输入流,因此不适合作为输出。您无法写入InputstreamgetResourceAsStream 返回用于读取资源的流,而不是写入资源。

恐怕没有简单的方法可以写入通过 ClassLoader 加载的资源。一种解决方案是将其读作 Properties,然后使用store方法将其写入资源文件,通过其他方式获取输出流。您可以使用类加载器获取资源文件的 URI,但不能保证您可以直接写入它(例如,如果它捆绑在签名的 jar 中)。

InputStream represents an input stream, so it is not suitable as ouput. You cannot write to an Inputstream. getResourceAsStream return a stream for reading a resource, not for writing to it.

I'm affraid there is no easy way to write to resource loaded via the ClassLoader. One solution would be to read it as Properties, and then use the store method to write it to a resource file, obtaining the output stream by other means. You can obtain the URI of the resource file using the classloader, but there is no guarantee you can write to it directly (for example if it is bundled in a signed jar).

北方。的韩爷 2024-09-16 11:02:00

首先,要写入流,您需要一个通用的流写入器,而不是文件写入器。其次,写作意味着输出,所以你需要一个输出编写器。因此,您要查找的类是 OutputStreamWriter

getResourceAsStream 返回一个 InputStream,您只能将其用于读取,这意味着您只能使用诸如 InputStreamReader

更新:

您已经有了使用 FileWriter 附加到文件的正确代码。但是,getResourceAsStream 返回只读资源,因此没有直接的方法将数据写回其中。

First of all, to write to a stream, you need a generic stream writer, not a file writer. Secondly, writing means output, so you need an output writer. So the class you're looking for is OutputStreamWriter.

getResourceAsStream returns an InputStream which you can only use for reading, which means you can only use a class such as InputStreamReader.

Update:

You already have the correct code for appending to a file, using FileWriter. However, getResourceAsStream returns a read-only resource, so there is no straightforward method to write data back to it.

猥琐帝 2024-09-16 11:02:00

如果您阅读文档,

if you read the docu, here, you will see, that there is no constructor for your InputStream. Thats why i does not compile. Since you did not ask anything, thats it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文