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.