Java InputStreamReader and OutputStreamWriter

Source: Internet
Author: User

I would like to write down the usage of these two classes, because they are the only input output classes that I found in the reader and writer class hierarchies that can specify a character encoding set (Charset). Here are two classes that give an example of how they are used, respectively, as decorators of InputStream and OutputStream.

public void Test1 () {InputStream in; Charset Charset = Charset.forname ("UTF-8"); try {in = new FileInputStream ("Test1.txt");//Charset Specifies the character encoding set of the characters stream reader reader = new InputStreamReader (in, charset); int Temp;while ((temp = Reader.read ())! =-1) {char c = (char) temp; System.out.print (c);}} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();}}


public void Test2 () {Charset Charset = charset.forname ("UTF-8"); try {outputstream outputstream = new FileOutputStream ("Te St1.txt ");//charset the character encoding set specified by the character stream output writer writer = new OutputStreamWriter (OutputStream, CharSet); Writer.write (" < Span><span class= "string" >liyuncong Wangfang Lee </span></span> "); Writer.flush ();} catch (IOException  e) {//TODO auto-generated catch Blocke.printstacktrace ();}}

this way, the auxiliary characters can also be handled correctly.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java 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.