Summary of Java Foundation-io (ii) outline pending update

Source: Internet
Author: User
Tags readline

First, the use of buffer streams

  Each byte stream has a corresponding buffer stream:

  

  Bufferedinputstream/bufferedoutputstream

  Constructor:

  

  

  The method summary is similar to the corresponding node stream

Using buffered streams for File replication: in practice

@Test Public voidtestBuffered1 () {//also need to associate files first, note text using reader Writer, non-text using FIS fosFile file1 =NewFile ("D:\\test\\1.jpg"); File file2=NewFile ("D:\\test\\2.jpg"); Bufferedinputstream bis=NULL; Bufferedoutputstream Bos=NULL; Try {            //Create a node streamFileInputStream FIS =NewFileInputStream (FILE1); FileOutputStream Fos=NewFileOutputStream (file2); //wrapping a node stream as a buffered streambis =NewBufferedinputstream (FIS); Bos=NewBufferedoutputstream (FOS); //prepare the buffered array            byte[] bytes =New byte[20]; intLen;  while(len = bis.read (bytes))! =-1) {bos.write (bytes,0, Len); //After you finish, refresh the finalBos.flush (); }        } Catch(IOException e) {e.printstacktrace (); } finally {            //Close the stream, pay attention to the closing order of the stream, and automatically close the corresponding node stream when the buffer stream is closed directly            if(Bos! =NULL) {                Try{bos.close (); } Catch(IOException e) {e.printstacktrace (); }            }            if(bis! =NULL) {                Try{bis.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }
View Code

 The use of BufferedReader and BufferedWriter is similar to the corresponding node stream

  Demonstrates a unique ReadLine method:

@Test Public voidTestbufferedreader () {File file1=NewFile ("D:\\test\\hello.txt"); BufferedReader BR=NULL; Try{FileReader fr=NewFileReader (FILE1); BR=NewBufferedReader (FR); //operation using BR-Unique read lineString s =NULL;  while((s = br.readline ())! =NULL) {System.out.println (s); }        } Catch(IOException e) {e.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }
View Code

  Note that a byte stream is required to read the doc document (even if the document is all text, Doc is no longer a plain text file)

Second, the use of the conversion stream

  Inputstreamreader/outputstreamwriter

  The JDK is described below: the process of coding and decoding needs to be clarified

InputStreamReader is a bridge of byte flow to a character stream: it reads the bytes with the specified charset and decodes them to characters. The character set it uses can be specified or explicitly given by name, or it can accept the default character set of the platform. Each call to a read () method in InputStreamReader causes one or more bytes to be read from the underlying input stream. To enable a valid conversion from byte to character, you can read more bytes in advance from the underlying stream to exceed the bytes required to satisfy the current read operation. In order to achieve maximum efficiency, it is necessary to consider packaging InputStreamReader within the BufferedReader. Example: BufferedReader in=NewBufferedReader (NewInputStreamReader (system.in)); OutputStreamWriter is a bridge of character flow to a byte stream: You can encode the characters in the stream to be written into bytes using the specified charset. The character set it uses can be specified by name or explicitly given, otherwise the platform default character set will be accepted. Each call to the write () method causes the encoding converter to be called on the given character (or character set). The resulting bytes are accumulated in the buffer before writing to the underlying output stream. You can specify the size of this buffer, but the default buffer is large enough for most purposes. Note that the characters passed to the write () method are not buffered. For maximum efficiency, consider wrapping the outputstreamwriter into BufferedWriter to avoid frequent calls to the converter. Example: Writer out=NewBufferedWriter (NewOutputStreamWriter (System.out)); The proxy pair is a character, it consists of twoCharValue Sequence representation: The range of the high surrogate is ' \ud800 ' to ' \UDBFF ', followed by the range ' \udc00 ' to ' \udfff 'Low-surrogate. The error proxy element refers to a high surrogate that is not followed by a low surrogate, or a low surrogate that has no previous high surrogate. This class always replaces the error proxy element and the non-mapped character sequence with the default substitution sequence for the character set. If you need more control over the encoding process, you should use the Charsetencoder class. 
View Code

  

  Decoding, the solution to what we can understand, that is, to solve the string

A simple example is as follows: (not particularly important, not specifically introduced here)

@Test Public voidtest1 () {File file=NewFile ("D:\\test\\hello.txt"); //Exception Handling TemporaryFileInputStream FIS =Newfileinputstream (file); //decoding of Byte stream to character streamsInputStreamReader ISR =NewInputStreamReader (FIS, "GBK"); BufferedReader BR=NewBufferedReader (ISR);        String s; //the operation of the buffered stream is omitted}
View Code

III. standard input/output stream

  Which is our common syetem.out/in.

  Example of receiving user input using:

@Test Public voidtest1 () {bufferedreader br=NULL; Try{InputStream in=system.in; //convert into a character streamInputStreamReader ISR =NewInputStreamReader (in); BR=NewBufferedReader (ISR);            String s;  while(true) {System.out.println ("Please enter a string:"); S=Br.readline (); if("Exit". Equalsignorecase (s)) {                     Break; } String S1=s.touppercase ();            System.out.println (S1); }        } Catch(IOException e) {e.printstacktrace (); } finally {            if(BR! =NULL) {                Try{br.close (); } Catch(IOException e) {e.printstacktrace (); }            }        }    }
View Code

Summary of Java Foundation-io (ii) outline pending update

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.