Java IO-memory operation stream

Source: Internet
Author: User


Use it to complete a case-insensitive Conversion Program:

Import java. io. *; public class ByteArrayDemo01 {public static void main (String args []) {String str = "HELLOWORLD"; // defines a String consisting of all uppercase letters: ByteArrayInputStream bis = null; // memory input stream ByteArrayOutputStream bos = null; // Memory output stream bis = new ByteArrayInputStream (str. getBytes (); // output content to memory bos = new ByteArrayOutputStream (); // prepare to read content from memory ByteArrayInputStream int temp = 0; while (temp = bis. read ())! =-1) {char c = (char) temp; // The number read is changed to the bos character. write (Character. toLowerCase (c); // converts the character to lowercase} // all data is stored in ByteArrayOutputStream String newStr = bos. toString (); // retrieve the content try {bis. close (); bos. close ();} catch (IOException e) {e. printStackTrace ();} System. out. println (newStr );}};
Conclusion: 1. the operation object of the memory operation flow must be based on the memory. Do not use the program as the standard. 2. In fact, you can instantiate OutputStream or InputStream by transforming up.
Import java. io. *; public class ByteArrayDemo01 {public static void main (String args []) throws Exception {String str = "HELLOWORLD"; // defines a String, inputStream bis = null is composed of uppercase letters; // memory input stream OutputStream bos = null; // Memory output stream bis = new ByteArrayInputStream (str. getBytes (); // output content to memory bos = new ByteArrayOutputStream (); // prepare to read content from memory ByteArrayInputStream int temp = 0; while (temp = bis. read ())! =-1) {char c = (char) temp; // The number read is changed to the bos character. write (Character. toLowerCase (c); // converts the character to lowercase} // all data is stored in ByteArrayOutputStream String newStr = bos. toString (); // retrieve the content try {bis. close (); bos. 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 the output location. If it is a file, FileXxx is used. If it is memory, 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.