With UTF-16 encoded characters in Java (see Bowen Java correctly traversing strings), the CharSet class establishes mappings between UTF-16 encoded byte sequences and byte sequences of other character encodings. When reading from the outside of the byte stream that represents the character, we can use the CharSet class to specify the encoding of the original character, so that the program can correctly convert the original byte encoding of the character into Java's own byte encoding, as well, when the character is written out, we can specify the character encoding by CharSet. Here are two simple examples to illustrate the use of CharSet.
public void Test1 () {Charset Charset = charset.forname ("UTF-8"); try {inputstream inputstream = new FileInputStream ("Test1 . txt "); int num = inputstream.available (); System.out.println ("Available Bytes number is:" + num); byte[] bytes1 = null;if (num > 0) {bytes1 = new Byte[num]; System.out.println ("Bytes1 length is:" + bytes1.length); Inputstream.read (bytes1);// CharSet specifies the original character encoding set of the byte array System.out.println (new String (Bytes1, CharSet));}} 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 "); String comparestring = "Liyuncong wangfang li";//charset specifies character encoding set for character encoding in string byte[] Bytes2 = Comparestring.getbytes (charset ); Outputstream.write (bytes2);} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();}}
in Java, the correct use of CharSet is very important, for example, when we download a large number of pages from the Internet, we need to find the correct encoding of the Web page and use the CharSet class to correctly transcode it when read, otherwise the downloaded page will have no meaning.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java uses the CharSet class to solve read-in character garbled problems and control output character encoding