[Java iO] _ memory operation stream notes

Source: Internet
Author: User

[Java iO] _ memory operation stream notes

Objectives of this Chapter
Master the usage of Memory Operation streams

Bytearrayinputstream and bytearrayoutputstream
In the program described earlier, both output and input come from the file. Of course, you can also set the output location on the memory. In this case, bytearrayinputstream and bytearrayoutputstream are used to complete the input and output functions.

Bytearrayinputstream mainly writes content to the memory, while
Bytearrayoutputstream mainly outputs data in the memory.

Format:
Public class bytearrayinputstream extends inputstream

Public class bytearrayoutputstream extends outputstream

Constructor:
Public bytearrayinputstream (byte [] BUF)

The following code uses the memory operation stream to convert uppercase and lowercase letters.

Import Java. io. *; public class bytearraydemo01 {public static void main (string [] ARGs) {string STR = "helloworld"; bytearrayinputstream Bis = NULL; bytearrayoutputstream Bos = NULL; bis = new bytearrayinputstream (Str. getbytes (); Bos = new bytearrayoutputstream (); int temp = 0; while (temp = bis. read ())! =-1) {char c = (char) temp; Bos. write (character. tolowercase (c); // reduce the number of characters} // all the data is in bytearrayoutputstream string newstr = Bos. tostring (); // retrieve the content try {bis. close ();} catch (ioexception e) {e. printstacktrace ();} system. out. println (newstr );}}

If you want to change a character to lowercase, you can use the packaging class: character

In fact, you can also instantiate outputstream or inputstream by transforming up.

Import Java. io. *; public class bytearraydemo02 {public static void main (string [] ARGs) throws exception {string STR = "helloworld"; inputstream Bis = NULL; outputstream Bos = NULL; bis = new bytearrayinputstream (Str. getbytes (); Bos = new bytearrayoutputstream (); int temp = 0; while (temp = bis. read ())! =-1) {char c = (char) temp; Bos. write (character. tolowercase (c); // reduce the number of characters} // all the data is in bytearrayoutputstream string newstr = Bos. tostring (); // retrieve the content try {bis. close ();} catch (ioexception e) {e. printstacktrace ();} system. out. println (newstr );}}

In fact, the above operations can reflect the object polymorphism. By instantiating different sub-classes of objects, the functions are also different, which is equivalent to different output. If it is a file, filexxx is used, if the memory is used, bytearrayxxx is used.

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.