IO stream of Java: memory stream, print stream, object stream

There are many input and output stream subclasses. See the following figure for details:

Used to manipulate memory

Bytearrayinputstream memory to the program does not need to be closed. It does not use memory resources. It is recommended not to use it if there is not enough memory

Bytearrayoutputstream program does not need to be closed to memory. It does not use memory resources. It is recommended not to use it if there is not enough memory

Memory input stream and memory output stream:

First, create a byte array. Generally, the reference data type is stored in memory. Obviously, at this time, you can create a byte array input stream and read it into the program.

<span style="color: #0000ff">import<span style="color: #000000"> java.io.ByteArrayOutputStream;

<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> ByteArrayOutputStreamDemo {

</span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;static</span> <span style="color: #0000ff"&gt;void</span><span style="color: #000000"&gt; main(String[] args) {
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; TODO Auto-generated method stub
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;1.创建字节数组<a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>流对象</span>
    ByteArrayOutputStream baos=<span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; ByteArrayOutputStream();
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;2.写入数据</span>
    baos.write(12<span style="color: #000000"&gt;);
    baos.write(</span>20<span style="color: #000000"&gt;);
    baos.write(</span>18<span style="color: #000000"&gt;);
    baos.write(</span>32<span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;3.<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/shuchu/" target="_blank" class="keywords">输出</a>流中的字节数据</span>
    <span style="color: #0000ff"&gt;byte</span>[] data=<span style="color: #000000"&gt;baos.toByteArray();
    </span><span style="color: #0000ff"&gt;for</span> (<span style="color: #0000ff"&gt;byte</span><span style="color: #000000"&gt; b : data) {
        Sy<a href="https://www.jb51.cc/tag/stem/" target="_blank" class="keywords">stem</a>.out.println(b);
    }
}

}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>