java中的FileWriter和InputStream
之前,当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
InputStream
表示输入流,因此不适合作为输出。您无法写入Inputstream
。getResourceAsStream
返回用于读取资源的流,而不是写入资源。恐怕没有简单的方法可以写入通过 ClassLoader 加载的资源。一种解决方案是将其读作
Properties
,然后使用store
方法将其写入资源文件,通过其他方式获取输出流。您可以使用类加载器获取资源文件的 URI,但不能保证您可以直接写入它(例如,如果它捆绑在签名的 jar 中)。InputStream
represents an input stream, so it is not suitable as ouput. You cannot write to anInputstream
.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 asProperties
, and then use thestore
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).首先,要写入流,您需要一个通用的流写入器,而不是文件写入器。其次,写作意味着输出,所以你需要一个输出编写器。因此,您要查找的类是
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 anInputStream
which you can only use for reading, which means you can only use a class such asInputStreamReader
.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.如果您阅读文档,
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.