(Copyright statement: self-created works, please do not forward! Otherwise, legal responsibility will be pursued.
Memories from last season:
Bytes: InputStream, OutputStream
Character stream: Reader, Writer
Practice questions from last quarter:
It is already a copy of the file, and bytes must be used, so create a byte input stream and then create a byte output stream, and write while reading.
Take a look at the effect:
IO operation is to study five classes and one interface
·File class
·OutputStream, InputStream
·Reader, Writer
· Target serialization
pipeline flow
Understand: The pipeline flow is the communication between the two threads.
publicvoidconnect(PipedOutputStreamsrc) throwsIOException
Connect the output to the input.
importjava.io.
;
//Set up two threads, one to send and the other to receive
classSendimplementsRunnable{
privatePipedOutputStreamout=null;
//Instantiate this target in the struct method
publicSend(){
this.out=newPipedOutputStream();
}
publicvoidrun(){
//Start the thread, send directly when the thread is started
Stringstr=HelloWorld~~~;
try{
this.out.write(str.getBytes());
}catch(Exceptione){}
}
//It is necessary to return the pipe output stream
publicPipedOutputStreamgetOut(){
returnthis.out;
}
}
classReceiveimplementsRunnable{
privatePipedInputStreaminput=null;
publicReceive(){
this.input=newPipedInputStream();
}
publicvoidrun(){
//Start the thread and receive the content
byteb=newbyte;
intlen=0;
try{
len=this.input.read(b);
this.input.close();
System.out.println(newString(b,0,len));
}catch(Exceptione){}
}
publicPipedInputStreamgetInput(){
returnthis.input;
}
}
publicclassIODemo01{
publicstaticvoidmain(Stringargs){
Sends=newSend();
Receiver=newReceive();
//Connect the output to the input~
try{
r.getInput().connect(s.getOut());
}catch(Exceptione){}
newThread(s).start();
newThread(r).start();
}
}
Take a look at the effect:
ByteArrayInputStream
Memory input and output streams
A lot of the content explained before is to output the content directly to the file, so now the content is input into the memory and output directly from the memory.
See if it's capitalized~
It should be understood as follows:
ByteArrayInputStream(): understood as memory to be read
ByteArrayOutputStream(): output from memory
I/O method
System.out:
-PrintStream is a print stream that provides better printing operations.
What are the benefits of PrintStream?
PrintStream is a subclass of OutputStream
In fact, there is a design pattern in IO—the decoration pattern.
·It is found that although the function in OutputStream can be output, the output process is not easy to operate, because all the content must be converted to byte array.
Fundamentals of PrintStream
PrintStream provides more printing functions than OutputStream, can output arbitrary data, and can also manage the output data to wrap.
Look at the effect: the newline on the screen is \n, and the newline in the file is \r\n.
Now that you have recognized that PrintStream is the target of OutputStream, then ask? Is it possible to terminate the function of outputting to the screen through OutputStream? You can use target polymorphism ~~~
PrintStream is a byte print stream, and similar to it there is a PrintWirter character print stream, it is more convenient to use a byte print stream.
Summarize
1. The connection between System.out and OutputStream
2. PrintStream is actually the use of JAVA decoration mode, which provides more and more convenient functions than the parent class
################################################## ##)