Mastering the memory operation Flow
Input and output are from the file, of course, you can also set the location of the output in memory , which requires Bytearrayinputstream and Bytearrayoutputstream
Bytearrayinputstream: writes content to memory ,
Bytearrayoutputstream: to output in-memory data
This should be done with memory as the operating point.
Use this class to accomplish some functionality.
Common methods
Bytearrayinputstream: Is the InputStream subclass.
extends InputStream
Construction Method:
Bytearrayinputstream (byte[] buf) creates a bytearrayinputstream, using BUF as its buffer array.
Receives a byte array, in fact the input to the memory is to pass the data into memory on the construction method.
Bytearrayoutputstream: Is OutputStream sub-class
The output is to write out the data from memory.
public class Bytearrayoutputstream extends OutputStream
Construction Method:
Bytearrayoutputstream () creates a new byte array output stream.
Main methods:
void write (int b)
Use them to complete a program that converts the order of uppercase and lowercase letters.
Revisit this method : GetBytes (); the definition is as follows: Returns an array of type byte .
Public byte[]getBytes() { return Stringcoding.encode (value, 0, value.length); }
1,
int size ()
2,
Tobytearray ()
3,
toString () Use the default character set of the platform to convert the buffer content to a string by decoding bytes
use them to complete a program that converts the order of uppercase and lowercase letters。
Casing conversion method for the string class :
String toLowerCase ()
Turn Capital:
String toUpperCase ()
If you want to put an uppercase edge lowercase, you can pass the wrapper class, the character wrapper class is character.
Static char tolowercase (char ch)
code example:
Note that unlike file reads, do not set the file path .
Package class set; Import java.io.*;p ublic class bytearraydemo01{public static void main (string args[]) { string str = "H Elloworld "; Defines a string that consists of all uppercase letters bis = NULL; Memory input stream bos = null; Memory output stream bytearrayinputstream(str.getbytes ()); Output content to memory, note that the file path is not set as if it were read. Bytearrayoutputstream(); Prepare to read the contents from memory Bytearrayinputstream, Note that it is not the same as the file read, do not set the file path .
int temp = 0; while ((Temp=bis.read ())!=-1) { char c = (char) temp;//The number read becomes a character Bos.write (character.tolowe Rcase (c)); Change the character to lowercase }//bos.tostring ();//Because all output data is in the Bytearrayoutputstream instance object, you can take out the contents, Converts the buffer contents to a string .
try{ bis.close (); Bos.close (); }catch (IOException e) { e.printstacktrace (); } System.out.println (NEWSTR); } };
In fact, the above operation is a good representation of the polymorphism of the object. By instantiating its subclasses differently, the completed functions are also different, which is equivalent to the location of the output,
If it is an output file, use the Filexxxx class. If it is memory, use BYTEARRAYXXX.
Java Fundamentals-Memory flow