How to solve the Chinese garbled characters

Source: Internet
Author: User
Java's Chinese problems often plague many developers. You may encounter the problem of passing Chinese parameters when developing Web applications. Finally, you may choose to use filter
The encoding method is set to GBK. We also encounter Chinese problems during the development of j2s, such as storing Chinese Characters in RMS, transmitting Chinese characters during network transmission, and reading Chinese characters from files.

When we solve the Chinese problem, the most used word is UTF-8. We know that the ASCII code is a single-byte encoding method, which can solve the English problem, but the Chinese font
It is very huge, and it is difficult to solve it with the asⅱ code. The Java language supports Unicode encoding. Unicode is a dual-byte encoding method that supports Chinese character libraries.
Not all strings are non-English characters. The UTF-8 encoding method can solve this problem, only when the character is not asⅱ code, he uses double byte to represent,
This saves space. Here I have summarized three common Chinese problems of j2m1.

Chinese problems in Network Transmission

The key to solving this problem is that we do not use inputstream or the method provided by outputstream to transmit data in bytes, but instead
Inputstream or outputstream is encapsulated as datainputstream and dataoutputstream. In this way, we can use
Writeutf (string
S). You can also use readutf () provided by datainputstream to read data. Pay attention to the sequence when using these two methods. For example, I
They may write such code online.

Client

Dos. writeint (Myint );

Dos. writebyte (mybyte );

Dos. writeutf (mystring );

Server

Int I = dis. readint ();

Byte B = dis. readbyte ();

String S = dis. readutf ();

RMS persistent storage Chinese problems

Because the data storage in RMS is stored in byte [] format, so we need to slightly change, but basically still take the idea of UTF-8.

Write Data

Bytearrayoutputstream baos = new bytearrayoutputstream ();

Dataoutputstream dos = new dataoutputstream (baos );

Dos. writeutf (mystring );

Byte [] DATA = baos. tobytearray ();

Rs. addrecord (data, 0, Data. Length );

Read data

Byte [] DATA = Rs. getrecord (INDEX );

Bytearrayinputstream BAIS = new bytearrayinputstream (data );

Datainputstream Dis = new datainputstream (BAIS );

String mystring = dis. readutf ();

Reading Chinese files

The file system is not supported in MIDP, but we can read the files in the jar package. If the file contains Chinese characters, we need to perform some special processing. First, we use UE
Or the notepad tool converts the relevant file to the UTF-8 format encoding. When reading files, we need to perform the following simple operations. The following are the processing functions.

Public String readfromfile (string filename)

{

String returnstring = NULL;

Inputstream is = getclass (). getresourceasstream (filename );

If (is! = NULL)

{

Bytearrayoutputstream baos = new bytearrayoutputstream ();

Int CH = 0;

Try

{

While (CH = is. Read ())! =-1)

{

Baos. Write (CH );

}

Byte [] DATA = baos. tobytearray ();

Returnstring = new string (data, "UTF-8 ");

Is. Close ();

Baos. Close ();

} Catch (ioexception E)

{

E. printstacktrace ();

}

}

Return returnstring;

}

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.