Java Core class library-io-byte array stream/memory stream

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.