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