InputStreamReader and OutputStreamWriter

Source: Internet
Author: User

Enter the conversion stream for the byte stream: InputStreamReader is a bridge of byte flow to character stream

conversion stream for output byte stream: OutputStreamWriter can change the output byte stream into the output character streams.

The role of the transform stream:
1. If the current acquisition is a stream of bytes that needs to be converted to the flow of characters, then the conversion stream can be used. BYTE stream----> character stream
2. Use the conversion stream to specify the encoding table to read and write files.

//reads the file data using the input byte stream's conversion stream to specify the code table     Public Static voidReadTest2 ()throwsioexception{File File=NewFile ("F:\\a.txt"); FileInputStream FileInputStream=Newfileinputstream (file); //create a stream of streams for the byte stream and specify the code table to readInputStreamReader InputStreamReader =NewInputStreamReader (FileInputStream, "Utf-8"); Char[] buf =New Char[1024]; intLength = 0;  while(length = Inputstreamreader.read (BUF))!=-1) {System.out.println (NewString (buf,0, length)); }            }                        //use the output byte stream to specify the code table to write data     Public Static voidWriteTest2 ()throwsioexception{File File=NewFile ("F:\\a.txt"); //set up the output channel of the dataFileOutputStream FileOutputStream =Newfileoutputstream (file); //Converts the output byte stream into a character flow and specifies the encoding table. OutputStreamWriter OutputStreamWriter =NewOutputStreamWriter (FileOutputStream, "Utf-8"); Outputstreamwriter.write ("New China is good."); //Close ResourceOutputstreamwriter.close (); }                 Public Static voidWritetest ()throwsioexception{File File=NewFile ("F:\\a.txt"); FileOutputStream FileOutputStream=Newfileoutputstream (file); //convert the output byte stream to the output character streams. OutputStreamWriter OutputStreamWriter =NewOutputStreamWriter (FileOutputStream); Outputstreamwriter.write ("Good for everyone.");            Outputstreamwriter.close (); }                     Public Static voidReadtest ()throwsioexception{InputStream in= system.in;//gets the standard input stream. System.out.println ("Read characters:" + (Char) In.read ());//Read () can read only one byte at a time. //the byte stream needs to be converted into character streams. InputStreamReader InputStreamReader =NewInputStreamReader (in); //buffering classes that use character streamsBufferedReader BufferedReader =NewBufferedReader (InputStreamReader); String Line=NULL;  while(line = Bufferedreader.readline ())! =NULL) {System.out.println ("Content:" +Line ); }                    }

InputStreamReader and OutputStreamWriter

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.