InputStream和ByteArrayInputStream有什么区别? [英] What's the difference between InputStream and ByteArrayInputStream?

查看:892
本文介绍了InputStream和ByteArrayInputStream有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码摘自核心java卷1的java web start章节。

The following code is extracted from the java web start chapter of the core java volume 1.

     ByteArrayOutputStream out = new ByteArrayOutputStream();
     PrintStream printOut = new PrintStream(out);
     printOut.print(panel.getText());
     //panel.getText() return a String
     InputStream data = new ByteArrayInputStream(out.toByteArray());
     FileSaveService service = (FileSaveService) ServiceManager
           .lookup("javax.jnlp.FileSaveService");
     service.saveFileDialog(".", new String[] { "txt" }, data, "calc.txt");

创建了四个对象,流重定向三次。
是否有其他方法可以使用jnlp api将数据写入文件?
InputStream和ByteArrayInputStream之间的区别是什么?

There are four objects created ,the stream is redirected three times. Is there any other methods to write data to a file by using jnlp api? what's the difference between InputStream and ByteArrayInputStream?

推荐答案

ByteArrayInputStream ByteArrayOututStream 是内存中的实现,当你想以类似流的方式暂时将数据存储在内存中,然后在其他地方再次将它抽出时。

ByteArrayInputStream and ByteArrayOututStream are in-memory implementations for use when you want to temporarily store the data in memory in a stream-like fashion, then pump it out again somewhere else.

例如,我们假设您有一个方法需要输入流作为参数,例如

For example, let's assume you have a method that expects an input stream as a parameter, eg

public Document parseXml(InputStream in) // build an XML document from data read in

但你想发送一个String的内容给它。然后你使用 ByteArrayInputStream 并用String的内容填充它,并将 ByteArrayInputStream 传递给该方法。

but you want to send the contents of say a String to it. Then you'd use a ByteArrayInputStream and fill it with the contents of your String and pass the ByteArrayInputStream to the method.



一个 ByteArrayOutputStream 用法的示例可能是方法写入输出流,但你只想捕获结果并直接得到它。


An example of an ByteArrayOutputStream usage might be if a method writes to an output stream, but you just want to capture the result and get it directly.

这篇关于InputStream和ByteArrayInputStream有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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