Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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");

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
253 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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.


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...