4. Classification of Chinese issues and its suggestion optimal solution
After understanding the principles of Java processing files, we can put forward a solution to the best solution to solve Chinese characters. Our goal is to: we have a Chinese string or a Chinese string or a Chinese-processed Java source program that can be translated into any other operating system, or get it to compile other operating systems. Correctly run, correctly transmit Chinese and English parameters, accurately communicate with database strings. 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. For this situation that is running directly on the CONSOLE, we recommend that when you write, if you need to receive the user from the user, you may have a Chinese input or an output of Chinese, the program should be used to process the input. And output, specifically, the following face-to-character node stream type: pair file: FileReader, FileWrieTer, FileInputStream, FileOutputStream pairs memory (array): ChararrayReader, CharaRrayWriter, CharaRrayWriter is: ByteArrayInputStream, ByteArrayOutputStream memory (string): StringReader, StringWriter pipeline: PipedReader, PipedWriter byte node whose stream type is: PipedInputStream, PipedOutputStream the same time, the input and output should be processed by the following processing flow for the character: BufferedWriter , the process flow is the byte which BufferedReader: BufferedInputeStream, process flow BufferedOutputStream InputStreamReader, OutputStreamWriter byte which is: DataInputStream, DataOutputStream wherein InputStreamWriter InputStreamReader and for converting the byte stream to the character stream according to the specified character code sets, Such as: InputStreamReader IN = New InputStreamReader (System.in, "GB2312"); OutputStreamWriter Out = New OutputStreamWriter (System.out, "GB2312"); for example: using the following example Java encoding requests:
//Read.java import java.io. *; public class read {public static void main (string [] args) throws ioException {string str = "/ n Chinese test, this is internal hard-coded string" "/ ntest english character "; String strin =" "; BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in," gb2312 ")); // input interface provided by the Chinese encoding BufferedWriter stdout = new BufferedWriter (new OutputStreamWriter (System.out, "GB2312")); // Set the output interface Press Chinese Code Stdout.Write ("Please enter:"); stdout.flush (); strin = stdin.readline (); stdout.write ("This is from User Enter String: " strin); stdout.write (STR); stdout.flush ();}} At the same time, we use the following ways when compiling: javac -encoding gb2312 read.java running results as shown in Figure 5 Schedule: Figure 52, support class for EJB and cannot run directly (such as JavaBean class)
Since this class itself is called, it is not directly interacting with the user. Therefore, for this type, our proposed handling method is a Chinese string that should be used in the internal program to handle the interior of the program (specifically as above As in one section), while compiling the class -Encoding GB2312 parameter indication source file is the Chinese format encoding.
3, targeting the servlet class
For servlet, we recommend the following methods:
When compiling the source program of the Servlet class, use -encoding to specify the encoded as GBK or GB2312, and the encoding portion when outputting to the user is set to setContentType ("text / html; charset = GBK"); or GB2312 to set the output In encoding format, when you receive user input, we use Request.setCharacterencoding ("GB2312"); this can be displayed correctly regardless of our Servlet class transplantation, only client browser supports Chinese display, you can display 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) throws IOException, ServletException {request.setCharacterEncoding ( "GB2312"); // set the input encoding format response.setContentType ( "text / html; charset = GB2312"); // set the output encoding format PrintWriter out = Response.getwriter (); // Recommended using PrintWriter output Out.println ("
Test this Servlet program as follows: <% @ page contenttype = "text / html; charset = GB2312"%> <% Request.setCharacterencoding ("GB2312");%>