4. Classification of Chinese issues and its suggestion optimal solution
After understanding the principles of the above Java handling files, we can put forward a suggestible to solve the Chinese character problem.
Method.
Our goal is to: we have a Chinese string or a Chinese string or a Chinese-handed Java source program edited in the Chinese system.
After compiling, you can move correctly in any other operating system, or you can correctly run after compiling in other operating systems, you can correctly pass the Chinese and English parameters, accurately communicate with the database to communicate with the database.
Our specific ideas are: in the port and exports of the Java program transcoding and the Java program limit the encoding method to limit the encoding method to the user.
The specific solution is as follows:
1. A class that is running directly on the Console
In this case, we recommend that when the program is written, if you need to receive users from the user, it may contain Chinese.
Enter or contain the Chinese output, and the character flow should be used in the program to process input and output. Specifically, apply the following to the characteristic node stream type:
For files: FileReader, FileWrieter
Its byte node stream is: FileInputStream, FileOutputStream
For memory (arrays): ChararrayReader, ChararrayWriter
Its byte nodes are: byterrayinputstream, ByteaRrayoutputstream
For memory (string): StringReader, StringWriter
Tongue: PipedReader, PiPedWriter
Its byte nodes are: pipedinputstream, pipedoutputstream
At the same time, you should use the following to the characteristic processing flow to process the input and output:
BufferedWriter, BufferedReader
Its byte type processing flow is: bufferedInputStream, BufferedoutputStream
InputStreamReader, OutputStreamwriter
Its byte type processing flow is: DataInputStream, DataOutputStream
The InputStreamReader and InputStreamWriter are used to convert byte streams according to the specified character encoding set.
To character stream, such as:
InputStreamReader IN = New InputStreamReader (System.in, "GB2312");
OutputStreamWriter out = new outputStreamWriter (System.out, "GB2312");
For example: the following example Java coding has reached the requirements:
//Read.java
Import java.io. *;
Public class read {
Public static void main (string [] args) throws oException {
String str = "/ n Chinese test, this is internal hardcoded string" "/ ntest english character";
String strin = "";
BufferedReader stdin = new buffredreader (New InputStreamReader (System.in, "GB2312")); // Setting Input Interface Press Chinese Coding
BufferedWriter stdout = new buffsetwriter (New OutputStreamwriter (System.out, "G
B2312 ")); // Set the output interface Press Chinese encoding
STDOUT.WRITE ("Please enter:");
STDOUT.FLUSH ();
Strin = stdin.readline ();
Stdout.write ("This is the string input from the user:" Strin);
STDOUT.WRITE (STR);
STDOUT.FLUSH ();
}
At the same time, when compiling the program, we use the following way:
Javac -ENCODING GB2312 Read.java
Its operation results are shown in Figure 5:
Figure 5
2, support for EJB classes and not directly running (such as Javabean classes)
Because this type itself is called by other classes, it is not directly interacting with the user, so our construction is for this kind.
The processing method of the discussion is that the character stream should be used in the internal program to handle the Chinese string inside (as in the above section), while in the compiling class, using the -Encoding GB2312 parameter indication source file is the Chinese format encoded. can.
3, targeting the servlet class
For servlet, we recommend the following methods:
When compiling the source program of the Servlet class, specify the encoded as GBK or GB2312 with -Encoding, and output to the user
The encoding section uses the setContentType ("text / html; charset = GBK") of the Response object; or GB2312
Set the output encoding format, and when you receive user input, we use Request.SetCharacterencoding ("GB231
2 "); this way, whether our Servlet class is transplanted in what operating system, only the client's browser supports Chinese display
It can be displayed correctly. The following is a correct example:
//Helloworld.java
Package hello;
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Public Class HelloWorld Extends httpservlet
{
Public void init () throws servletexception {}
Public void doget (httpservletRequest Request, HttpservletResponse Response) THR
OWS IOException, ServletException
{
Request.setCharacterencoding ("GB2312"); // Setting Input Coding Format
Response.setContentType ("text / html; charset = GB2312"); // Set the output encoding format
PrintWriter Out = response.getwriter (); // Recommended using PrintWriter output
Out.println ("
Out.println ("Hello World! this is created by servlet! Test Chinese);
Out.println ("
Public void dopost (httpservletRequest Request, HttpservletResponse Response) TH
Rows oException, servletexception
{
Request.setCharacterencoding ("GB2312"); // Setting Input Coding Format
Response.setContentType ("text / html; charset = GB2312"); // Set the output encoding format
String name = Request.getParameter ("name");
String ID = Request.getParameter ("ID");
IF (name == null) Name = ""
IF (id == NULL) ID = ""
PrintWriter Out = response.getwriter (); // Recommended using PrintWriter output
Out.println ("
Out.println ("The Chinese string you are incorporated is:" Name);
Out.println ("
Out.println ("
}
Public void destroy () {}
}
Please compile this program with Javac -Encoding GB2312 HelloWorld.java.
Testing this servlet is as follows:
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% Request.SetCharacterencoding ("GB2312");%>
Function Submit () {
// Pass the Chinese string value to servlet via URL
Document.base.Action = "./helloworld?name= Chinese";
Document.base.method = "post";
Document.base.submit ();
}
Script>
hEAD>