如何用StringReader替换StringBufferInputStream?
使用 JNLP,我有这一行:
File_Save_Service.saveFileDialog(null,null,new StringBufferInputStream("testing"),null);
How to Replace StringBufferInputStream with StringReader in this line ? StringBufferInputStream的API说最好使用StringReader,但是如何在不同类型之间进行转换?
Using JNLP, I have this line :
File_Save_Service.saveFileDialog(null,null,new StringBufferInputStream("testing"),null);
How to replace StringBufferInputStream with StringReader in this line ? The API for StringBufferInputStream says better use StringReader, but how to convert between different types ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
FileSaveService.saveFileDialog 采用 InputStream,而不是 Reader(因为它需要二进制数据,而不是字符数据)。
StringBufferInputStream 已被弃用,因为它不能正确地将字符转换为字节。
您可以使用 ByteArrayInputStream 代替:
FileSaveService.saveFileDialog takes an InputStream, not a Reader (because it wants binary data, not character data).
StringBufferInputStream is deprecated because it does not convert characters into bytes properly.
You can use ByteArrayInputStream instead:
标准 Java 类库无法将
Reader
包装为InputStream
,但以下 SO 问题及其答案提供了一些解决方案。请仔细阅读细则!如何将 Reader 转换为 InputStream 并一个 Writer to OutputStream?
但是,更简单的方法是坚持使用您所拥有的内容(如果这只是单元测试代码......就像它看起来的那样),或者替换
StringInputStream< /code> 与
ByteArrayInputStream
如 @Thilo 的答案所述。The standard Java class libraries offer no way to wrap a
Reader
as anInputStream
, but the following SO question and its answers offer some solutions. Be careful to read the fine-print!How to convert a Reader to InputStream and a Writer to OutputStream?
However, it is simpler to just stick with what you have got, (if this is just unit test code ... as it appears to be), or to replace the
StringInputStream
with aByteArrayInputStream
as described by @Thilo's answer.