Memory Stream (Array stream):
The data is temporarily present in the array and will be retrieved from the array later.
1. Byte memory stream: Bytearrayinputstream/bytearrayoutputstream
2. Character Memory stream: Chararrayinputstream/chararrayoutputstream
3. String stream: Stringreader/stringwriter (storing data in an array)
BYTE memory stream:
Public classBytearraydemo { Public Static voidMain (java.lang.string[] args)throwsException {//byte array output stream: program-to-memoryBytearrayoutputstream BOS =NewBytearrayoutputstream (); Bos.write ("ABCD". GetBytes ()); //using the stored temporary data byte[] buffer =Bos.tobytearray (); //byte array input stream: Memory to programBytearrayinputstream bis =Newbytearrayinputstream (buffer); byte[] Bys =New byte[1024]; intLen =-1; while(len = Bis.read (bys))! =-1) {System.out.print (NewJava.lang.String (bys,0, Len)); } bos.close (); Bis.close (); }}
Character array flow: roughly the same as the byte operation.
String Flow:
1 Public classStringwriterreaderdemo {2 Public Static voidMain (string[] args)throwsIOException {3 //the output stream of the string4StringWriter StringWriter =NewStringWriter ();5Stringwriter.write ("How old are You?") ");6Stringwriter.write ("21 years old! ");7 8 //input stream of the string9StringReader StringReader =NewStringReader (stringwriter.tostring ());Ten Char[] buffer =New Char[1024]; One intLen =-1; A while(len = stringreader.read (buffer))! =-1){ -System.out.println (NewString (buffer,0, Len)); - } the } -}
Java Core class library-io-byte array stream/memory stream