Some understanding of Java coding Problems

Source: Internet
Author: User
Tags return string linux
Coding | Problems Java language default encoding is Unicode, and we Chinese commonly used files and databases are based on GB2312 or BIG5 encoding, how to properly select the encoding method and correctly handle the Chinese character encoding? This article will start with encoding common sense, combine Java programming example, analyze above two questions and propose solution to them:

1. Add a statement to your JSP program:
<% @page contenttype= "text/html;charset=gb2312"%>

2. Ask for a string encoding problem at the URL.
If the information passed from the client through the Get/post method contains Chinese character information, servlet/jsp cannot get the correct value.
We specify the encoding that the application wants before calling Request.getparameter ("Param_name").
which is request.setcharacterecoding ()

3. The problem of coding on different platforms is different.
The standard on Linux platform is Iso8859_1, and under Win2K is GBK, these are the default standards, if your server is not like this, then compile the problem will be problematic. I encountered such a problem, two Linux servers, a lang=en, a lang=en,utf8, I have been doing n long time to find out why.

3. I myself use the most conversion coding of a class (on the Linux platform), almost all of the coding problems can be solved. The method of the class is very resolved. The main ASC2GB () and GB2ASC () are the two classes.
Package com.whaic.tools;

Import java.io.UnsupportedEncodingException;

public class Ecov
{
public static string ASC2GB (string asc) {
String ret;

if (asc==null) return ASC;
try{
Ret=new String (asc.getbytes ("Iso8859_1"), "GB2312");
}
catch (Unsupportedencodingexception e) {
RET=ASC;
}
return ret;
}

public static string Gb2asc (String GB) {
String ret;
if (Gb==null) return GB;
try{
Ret=new String (gb.getbytes ("GB2312"), "iso8859_1");
}
catch (Unsupportedencodingexception e) {
RET=GB;
}
return ret;
}
}


4. Chinese issues when reading and writing files:

Read::

FileInputStream fis = new FileInputStream (strinfile);

InputStreamReader ISR = new InputStreamReader (FIS, "GB2312");

Reader in = new BufferedReader (ISR);

int ch;

while (ch = in.read ()) >-1) {

Icharnum + 1;

Buffer.append ((char) ch);

}

In.close ();


Write::

FileOutputStream fos = new FileOutputStream (stroutfile);

Writer out = new OutputStreamWriter (FOS, "Big5");

Out.write (str);

Out.close ();

The above is only my daily use of some of the problems encountered and solutions. Since internationalization is not done domestically, the support for Chinese characters is not as perfect as the Java Soft claims to be without rigorous testing before these basic classes are released. Java programming language grew up in the Web world, which requires Java to have good support for multinational characters. The Java programming language adapts the computational network demand, has laid the solid foundation for it to grow rapidly in the network world. The Java Creator (Java Soft) has taken into account the Java programming language's support for multinational characters, but there are a lot of flaws in the solution now, and we need to put some compensatory measures into it. And the World Organization for Standardization is trying to unify all human words in one encoding, one of which is ISO10646, which represents one character in four bytes. Of course, before this scenario is adopted, it is hoped that the Java Soft can rigorously test its products and bring more convenience to the user.


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.