We need to know that in addition to files , the source and destination of the stream can also be computer memory .
The byte array input stream ByteArrayInputStream and the byte array output stream ByteArrayOutputStream use byte arrays as the source and destination of the stream respectively.
ByteArrayInputStream(byte[]buf);ByteArrayInputStream(byte[]buf,intoffset,intlength);
The source of the byte array stream constructed by the first constructor is all the byte units of the array specified by parameter buf; the source of the byte array stream constructed by the second constructor is the array specified by buf, taken in order from offset. length byte units.
The byte array input stream calls public int read(); the method can sequentially read a byte from the source, and the method returns the read byte value; call public int read(byte[] b,int off,int len ); method can sequentially read the number of bytes specified by parameter len from the source, and store the read bytes into the array specified by parameter b. The parameter off specifies the starting position of array b to store the read bytes. This method returns the actual number of bytes read. If no bytes have been read, the read method returns -1.
ByteArrayOutputStream();ByteArrayOutputStream(intsize);
The byte array output stream constructed by the first constructor points to a buffer with a default size of 32 bytes. If the number of bytes written by the output stream to the buffer is greater than the buffer, the buffer's capacity will automatically increase.
The initial size of the buffer pointed to by the byte array output stream constructed by the second constructor is specified by the parameter size. If the number of bytes written by the output stream to the buffer is greater than the buffer, the buffer's capacity will automatically increase.
The byte array output stream calls public void write(int b); the method can write a byte to the buffer sequentially; the method calls public void write(byte[] b, int off, int len); the method can write parameter b in The specified len bytes are written into the buffer sequentially, and the parameter off specifies the starting position of the bytes written from b; calling the public byte[] toByteArray(); method can return all the output streams written to the buffer. byte.
Corresponding to the byte array stream are the character array stream CharArrayReader class and CharArrayWriter class. The character array stream uses character arrays as the source and destination of the stream respectively.
For example, use an array stream to write "mid-autumn festival" and "Happy Mid-Autumn Festival" to memory (the buffer of the output stream), and then read the written data from memory:
importjava.io.*;publicclassMain{publicstaticvoidmain(Stringargs[]){try{ByteArrayOutputStreamoutByte=newByteArrayOutputStream();byte[]byteContent=mid-autumnfestival.getBytes();outByte.write(byteContent);ByteArrayInputStreaminByte=newByteArrayInputStream(outByte.toByteArray ());bytebackByte[]=newbyte[outByte.toByteArray().length];inByte.read(backByte);System.out.println(newString(backByte));CharArrayWriteroutChar=newCharArrayWriter();char[]charContent=Mid-Autumn Festival Happy.toCharArray();outChar.write(charContent);CharArrayReaderinChar=newCharArrayReader(outChar.toCharArray());charbackChar[]=newchar[outChar.toCharArray().length];inChar.read(backChar);System.out. println(newString(backChar));}catch(IOExceptionexp){}}}