Good memory than bad pen, take Java io to say, this part of the basic Class I have learned the university once, but now forget almost, so prepare to write a blog, say these things are recalled, and tidy up a bit.
First borrow a picture from the Internet:
Vertically divided into byte stream and character stream. Horizontal partitioning for reading and writing
In this picture of those very basic also do not mention, mention the need to pay attention to several classes.
1.BufferedXXX Buffering Whether it is read or write, character or byte stream exists, mainly as a buffer function, play the role is to reduce and operating system IO interaction, reduce performance consumption
2.PipedXXX Pipeline Pipeline is also the existence of these four, the usage of the pipeline is equivalent to the queue that we use, you plug in the data, then from the other end can be taken out, otherwise it will be blocked, the following uses the stream of characters as an example:
Packagecom.hotusm.io.learn.string;ImportJava.io.PipedReader;ImportJava.io.PipedWriter;/*** Character Stream Remove pipe * 1.PipedReader (pipedwriter src, int pipesize) The first parameter is the input pipeline flow the second parameter is the size of the pipe * if there is no second parameter then the default pipe size is 1024x768 * 2.Piped Reader () The constructor with no parameters requires a call to connect (pipedwriter src) * 3.read () to a state that has not yet been initialized to be blocked until a data stream arrives and then reads*/ Public classPipedreadertest { Public Static voidMain (string[] args)throwsexception{testpipedreaderconnect (); } Public Static voidTestpipedreaderconnect ()throwsexception{Pipedreader Pipedreader=NewPipedreader (); FinalPipedWriter writer=NewPipedWriter (); //connect in this wayPipedreader.connect (writer); Char[] buff=New Char[10]; NewThread ((){ Try { //after three seconds of stayThread.Sleep (3000); Writer.write ("Hello piped"); } Catch(Exception e) {e.printstacktrace (); }}). Start (); Pipedreader.read (Buff); SYSTEM.OUT.PRINTLN (Buff); }}
。
3. Unique InputStreamReader and OutputStreamWriter in the character stream. Use the adapter mode to fit the byte stream into a character stream, which allows you to use byte streams as character flows.
4. The pushbackxxx that are unique to the input stream are inherited from the xxxfilter, and they are all = features that use the adornment mode to add a fallback when reading. The following characters are used to show their effect:
Packagecom.hotusm.io.learn.string;ImportJava.io.CharArrayReader;ImportJava.io.PushbackReader;/*** Pushbackreader is able to push the stream or character back into the buffer. 1.PushbackReader (Reader reader,int size) * Size: Each time the number of read (), the default is 1 is also the size of the buffer 2.unread () overloaded method * Pushes one or more words Fu Hui into the buffer, The position is the last read of the end, such as ABCD Read C now launched GF so that is for abcgfd next read the words will start from * GF Read 3.skip (long size) skip size characters*/ Public classFilterreaderandpushbackreadertest { Public Static voidMain (string[] args) {Testfilterreaderunreadsinglechar (); System.out.println (); Testfilterreaderunreadmutilchar (); Testfilterreaderskip (); } /*** Output: ABCCD*/ Public Static voidTestfilterreaderunreadsinglechar () {String str= "ABCD"; Try(CharArrayReader CharArrayReader =NewCharArrayReader (Str.tochararray ()); Pushbackreader Pushbackreader=NewPushbackreader (chararrayreader);) { intC; while((c = Pushbackreader.read ())! = 1) {System.out.print (Char) c); //the use of unread () pushes the character back into the buffer if(c = = ' C ') {Pushbackreader.unread (C); } } } Catch(Exception e) {e.printstacktrace (); } } /*** Output: Abcdeffucguc*/ Public Static voidTestfilterreaderunreadmutilchar () {String str= "ABCDEFG"; Try(CharArrayReader CharArrayReader =NewCharArrayReader (Str.tochararray ()); Pushbackreader Pushbackreader=NewPushbackreader (CharArrayReader, 3);) { Char[] Bytearr =New Char[3]; //The read method will always read the number of characters in the second argument in the constructor while((Pushbackreader.read (bytearr))! =-1) {System.out.print (Bytearr); //the use of unread () pushes the character back into the buffer if(NewString (Bytearr). Equals ("Def")) { //pushing back a buffer that cannot be greater than a buffer is the second parameter of our constructorPushbackreader.unread ("FUC". ToCharArray ()); } } } Catch(Exception e) {e.printstacktrace (); } } /*** Output: abcfg*/ Public Static voidTestfilterreaderskip () {String str= "ABCDEFG"; Try(CharArrayReader CharArrayReader =NewCharArrayReader (Str.tochararray ()); Pushbackreader Pushbackreader=NewPushbackreader (CharArrayReader, 3);) { Char[] Bytearr =New Char[3]; //The Read method is read into the second argument in the constructor. while((Pushbackreader.read (bytearr))! =-1) {System.out.print (Bytearr); //here's the point!!!Pushbackreader.skip (2L); Bytearr=New Char[3]; } } Catch(Exception e) {e.printstacktrace (); } }}
5. The two printwriter and PrintStream, which are unique in the output stream, differ from the previous one, except that the two are characters, one is a byte, and there are other differences. They also want some similarities. The following hit show.
When we use PrintWriter, but we set the ability to automatically refresh, then only when the Println,printf,format method call will work, this and the printstream of the byte stream is different. Here is the description in the source code:
class if * It'll be done with only one of the <tt>println</tt>, <tt>printf</tt>* <TT >format</tt>* happens to is output. These methods use the platform ' s own notion of line * separator rather than the newline character.
The PrintStream is to call the println or the characters in the newline (' \ n ') will be automatically refreshed, which is a different point in the character stream. The source code describes this:
<code>PrintStream</code> This means the <code>flush</code> byte Byte * (<code> ' \ n ' </code>) is written.
Reference: http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html
Java-io Basic Memories