Java uses the CharSet class to solve read-in character garbled problems and control output character encoding

Source: Internet
Author: User

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

Related Article

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.