Java's Chinese issues usually plague many developers, you may have encountered the issue of Chinese parameters when developing web applications, and finally you may choose to use Filter to set the request to GBK. In the development of J2ME, we will also encounter Chinese issues, such as in the RMS, in the RMS, in the network transmission, the Chinese is transmitted, and Chinese and other issues are read from the file.
When we solve the Chinese problem, we use the most words used to use UTF-8. We know that the ASCII code is a single-byte coding method, which can solve the problem of English, but Chinese fonts are very large, and it is difficult to solve with the ASSII code. The Java language is to support Unicode encoding. Unicode is a double-byte encoding method to support Chinese branches, but this has brought some waste because all strings are non-English characters. UTF-8 encoding is just likely to solve this problem, and he uses double bytes when the character is not an ASSII code, which saves space. Here I only summarize three J2ME common Chinese issues.
Chinese issues in network transmission Solve this problem is that we do not use the method provided by InputStream or OutputStream followed by bytes, but encapsulate InputStream or OutputStream to DataInputStream and DataOutputStream. This allows us to use WriteUTF (String S) in DataOutputStream to send data, and you can read data using the readUTF () provided by DataInputStream. Note that you should pay attention to the order when using two methods. For example, we may write such code client dos.writeint (myint); dos.writeutf (mystring); server INT i = disp.readint (); Byte B = DIS .readcy (); string s = disp.readutf (); RMS persistent storage Chinese problem Since the data store in the RMS is stored in the form of Byte [], we need to change slightly, but basically go The idea of UTF-8. Writing 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 (); Chinese document in question reads File systems are not supported in MIDP, but we can read files in the JAR package. If there is a Chinese in the file, we need some special processes, first we use the UE or NOTEPAD tool to encode the related files UTF-8 format. We need to make the following simple processing when reading the file. Here is the processing function.
public String readFromFile (String fileName) {String returnString = null; InputStream is = getClass () getResourceAsStream (fileName);. if ByteArrayOutputStream baos = new ByteArrayOutputStream (! is = null) {(); int ch = 0; try {while ( (CH = is.read ())! = -1) {baos.write (ch);} byte [] data = baos.tobyteaRray (); returbystring = new string (DATA, "UTF-8"); IS. CLOSE (); baos.close ();} catch (ioException e) {E.PrintStackTrace ();}} Return Returnstring;