Java IO Four (use of conversion streams)

Source: Internet
Author: User

Pull something else: with age slowly grow, some reason just understand, some love just feeling. Your family may want you to start a career based on a big city. But in the heart of the family will always hope you spend more time with them, eat dinner, talk, as long as you safe, happy life, family feel happy. So spend a little more time with your parents, accompany your spouse, accompany your child.

also put on hold for a short time, continue to write the knowledge of Java IO, now feel their memory is really not, always remember things, hey, key or understanding not deep enough, by rote is no way to look like. Open up. Reference from Bi Xiangdong Teacher's video
First: InputStreamReader
Look at the JDK inside the explanation, stressed the importance of their own reading English documents, do not be afraid, afraid to never read English, adhere to the look!

class inputstreamreader java . lang   java.io   java.io   InputStreamReader is a bridge that converts bytes into character streams, reads the bytes and decodes them into characters using the specified character set. A character set can be specified by name or explicitly, or by using the default character set of the platform. Each call to a InputStreamReader read () method reads one or more bytes from the low-yielding byte stream, and in order to increase the efficiency of converting bytes to characters, it is possible to read more bytes in advance from the underlying byte stream to exceed the number of bytes required to satisfy the current read operation. To improve efficiency, use BufferedReader to wrap inputstreamreader, such as BufferedReader in  = new BufferedReader (New InputStreamReader (System.in ))   
ininininString

Second OutputStreamWriter

outputstreamwriter is a bridge of character flow to a byte stream, with characters flowing over the specified character set to encode characters into bytes. The character set can be specified either by name or explicitly, or by using the default character set of the platform. Each invocation of a writer () method causes the encoding converter that specifies the character set to be called, and the encoded bytes accumulate in a buffer before being written out into the underlying stream. The size of this buffer can be specified, generally using the default size of the line. Characters written by the write  () method are not cached. To be efficient, wrap the OutputStreamWriter with BufferedWriter () to avoid frequent calls to the converter. For example writer out  = new  bufferedwriter (new  outputstreamwriter (system. Out )); The following narrative should be about the garbled problem. Surrogate pairs are represented by a sequence of two char values: High Agent range from  to  ' \ UDBFF ' The  is followed by a low agent range from  to   
构造函数OutputStreamWriter(OutputStream out) 使用默认的字符编码构造一个OutputStreamWriter 实例。OutputStreamWriter(OutputStream out, Charset cs) 使用指定的字符集构造一个OutputStreamWriter 实例。OutputStreamWriter(OutputStream out, CharsetEncoder enc) 使用指定的字符编码器构造一个OutputStreamWriter 实例。OutputStreamWriter(OutputStreamString charsetName) 使用指定的字符集名称构造一个OutputStreamWriter 实例。

Use examples to print from the keyboard to the console;

 PackageJavase;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter; Public  class TransStreamDemo1 {//Throw exception     Public Static void Main(string[] args)throwsIOException {//Build a BufferedReaderBufferedReader bufferedreader=NewBufferedReader (NewInputStreamReader (system.in));//Build a BufferedWriterBufferedWriter bufferedwriter=NewBufferedWriter (NewOutputStreamWriter (System.out)); String line=NULL; while((Line=bufferedreader.readline ())! =NULL) {if("Over". Equals (line)) { Break;            } bufferedwriter.write (line); Bufferedwriter.newline ();//Line breakBufferedwriter.flush ();    } bufferedwriter.close (); }}

If you want to store the keyboard entry in a text file, you only need to change one line of code in the code.

new BufferedWriter(new OutputStreamWriter(new FileOutputStream("out.txt")));

In this way, the input from the keyboard will be stored in the project OUT.txt file.

If you want to print the contents of a text file to the console, simply change the source of the input stream, as shown below.

new BufferedReader(new InputStreamReader(new FileInputStream("in.txt")));

If you want to copy the contents of a text file into another text file, just specify the source and destination as a file. Copy the characters from the In.txt file to OUT.txt as shown below

new BufferedReader(new InputStreamReader(new FileInputStream("in.txt"new BufferedWriter(new OutputStreamWriter(new FileOutputStream("out.txt")));

Note that this is just to demonstrate the use of byte-character conversion streams, and if you really want to copy a text file, you can use a character stream to complete it. As shown below

new BufferedReader(new FileReader("in.txt"));        new BufferedWriter(new FileWriter("out.txt"));

In the episode, if you want to change the standard input and output stream, which is the keyboard and console, you can use the static method of the system.

staticvoidin"standard"staticvoidout"standard"

At the end of the day, there is not much knowledge, but watching TV, playing ball, and having a game in the afternoon is also difficult. My little waist ah, still pain.

Java IO Four (use of conversion streams)

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.