Java-IO basic class memories and java-io basic memories

Source: Internet
Author: User

Java-IO basic class memories and java-io basic memories

For Java I/O, I have already learned this basic category in college, but I forgot about it. So I am going to write a blog, let's take a look at all these things and sort them out.

First, use a picture on the Internet:

Vertical streams are divided into byte streams and byte streams. Horizontal streams are divided into read and write streams.

In this figure, we will not mention the very basic ones. We will mention several classes that need attention.

1. BufferedXXXBufferBoth read and write, character and byte streams exist. They serve as a buffer to reduce IO interaction with the operating system and reduce performance consumption.

2. PipedXXXMPs queuePipeline also exists among the four. The pipeline usage is equivalent to the queue we use. If you insert data into the queue, you can obtain the data from the other end. Otherwise, it will be blocked, the following uses the input stream of the callback stream as an example:

Package com. hotusm. io. learn. string; import java. io. pipedReader; import java. io. pipedWriter;/*** pipeline extract Pipeline * 1. pipedReader (PipedWriter src, int pipeSize) the first parameter is the second parameter of the input pipeline stream. * if there is no second parameter, the default pipeline size is 1024*2. the constructor without parameters in PipedReader () needs to call connect (PipedWriter src) * 3 to set the initialization status. read () is blocked until data streams arrive and then read */public class PipedReaderTest {public static void main (String [] args) throws Exception {testPipedReaderConnect ();} public static void testPipedReaderConnect () throws Exception {PipedReader pipedReader = new PipedReader (); final PipedWriter writer = new PipedWriter (); // This method is used to connect to pipedReader. connect (writer); char [] buff = new char [10]; new Thread ()-> {try {// Thread after three seconds. sleep (3000); writer. write ("hello piped");} catch (Exception e) {e. printStackTrace ();}}). start (); pipedReader. read (buff); System. out. println (buff );}}

.

3. UniqueInputStreamReader and OutputStreamWriter. Use the adapter mode to adapt the byte stream to the hidden stream, so that the byte stream can be used as the hidden stream.

4. unique in the input streamPushbackXXXBoth of them are inherited from XXXFilter. They both use the decoration mode to add the rollback function for reading. The following uses the character form to show their functions:

Package com. hotusm. io. learn. string; import java. io. charArrayReader; import java. io. pushbackReader;/*** PushbackReader can push the stream or character back to the buffer. 1. pushbackReader (Reader reader, int size) * size: the number of reads () each time. The default value is 1 and the buffer size is 2. unread () overload method * pushes one or more characters back to the buffer zone, the position is the end of the last read. For example, if ABC reads gf from c, then ABC GfD reads data starting from * gf next time. skip (long size) skips size characters */public class FilterReaderAndPushbackReaderTest {public Static void main (String [] args) {testFilterReaderUnreadSingleChar (); System. out. println (); testFilterReaderUnreadMutilChar (); testFilterReaderSkip ();}/*** output: abcCd */public static void testFilterReaderUnreadSingleChar () {String str = "abcd "; try (CharArrayReader charArrayReader = new CharArrayReader (str. toCharArray (); PushbackReader pushbackReader = new PushbackReader (charArrayReader );){ Int c; while (c = pushbackReader. read ())! =-1) {System. out. print (char) c); // use of unread () to push the character back to the buffer if (c = 'C') {pushbackReader. unread ('C') ;}} catch (Exception e) {e. printStackTrace () ;}}/*** output: abcdefFUCgUC */public static void testFilterReaderUnreadMutilChar () {String str = "abcdefg"; try (CharArrayReader charArrayReader = new CharArrayReader (str. toCharArray (); PushbackReader pushbackReader = new PushbackReader (charArr AyReader, 3);) {char [] byteArr = new char [3]; // The read method will always read the number of characters in the second parameter of the constructor while (pushbackReader. read (byteArr ))! =-1) {System. out. print (byteArr); // use of unread () to push the character back to the buffer if (new String (byteArr ). equals ("def") {// The buffer that cannot be pushed back is the second parameter of the constructor pushbackReader. unread ("FUC ". toCharArray () ;}} catch (Exception e) {e. printStackTrace () ;}}/*** output: abcfg */public static void testFilterReaderSkip () {String str = "abcdefg"; try (CharArrayReader charArrayReader = new CharArrayReader (str. toCharArray (); Pus HbackReader pushbackReader = new PushbackReader (charArrayReader, 3);) {char [] byteArr = new char [3]; // The read method will always read while (pushbackReader. read (byteArr ))! =-1) {System. out. print (byteArr); // here is the focus !!! PushbackReader. skip (2L); byteArr = new char [3] ;}} catch (Exception e) {e. printStackTrace ();}}}

5. Two unique output streamsPrintWriter and PrintStreamDifferent from the previous one, the two are not only characters, but also bytes. They also have some similarities. The following is a one-to-one display.

When we use PrintWriter, But we set it to automatically refresh, it will only take effect when the println, printf, and format methods are called, this is different from the PrintStream with throttling. The descriptions in the source code are as follows:

* <p> Unlike the {@link PrintStream} class, if automatic flushing is enabled * it will be done only when one of the <tt>println</tt>, <tt>printf</tt>, or * <tt>format</tt> methods is invoked, rather than whenever a newline character * happens to be output.  These methods use the platform's own notion of line * separator rather than the newline character.

However, PrintStream automatically Refreshes a println call or a line break ('\ n') in a character, which is different from that in a streaming. The source code is described as follows:

 <code>PrintStream</code> can be created so as to flush * automatically; this means that the <code>flush</code> method is * automatically invoked after a byte array is written, one of the * <code>println</code> methods is invoked, or a newline character or byte * (<code>'\n'</code>) is written.

 

Reference: http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html

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.