Java character encoding problem

Source: Internet
Author: User
Tags character set
Coding | Question 1. about file Encoding
General file Save (text file), at the time of save by the system default language encoding (character set) to save, in the Chinese system is GBK, English system is iso8859_1, Japanese system is MS932. The same is the same as the current system default language (character set) encoding to decode, so some Chinese characters in the Japanese system will display garbled reason is this. In other words, when you open a file, different platforms use different character sets, such as the Chinese character set including A,b,c, Japanese character set including A,b, when the Japanese system decoding the Chinese character C, it will appear the so-called garbled.

2. Java Work processing Coding mode
(1) Java in the runtime will be Unicode to store characters, so that the beneficial support of the multilingual environment.
(2) Java in the reading of the file by default is in accordance with the system default language (character set) encoding to decode the file, read and save when the coding inconsistency also results in the program parameter value error, read the file with the FileInputStream class can specify the encoding read
(3) Java in the output to the System interface (Windows) will be in the memory of variable characters again through the system default language (character set) encoding to convert, so in the output process will also encounter a series of coding problems

3. The solution
(1) SWING/AWT: Output to the system interface decoding can be set by setting the component display font to replace the system character set decoding
For example:
public static void SetFont (Font pfont) {
Uimanager.put ("Button.font", Pfont);
Uimanager.put ("Togglebutton.font", Pfont);
Uimanager.put ("Radiobutton.font", Pfont);
Uimanager.put ("Checkbox.font", Pfont);
Uimanager.put ("Colorchooser.font", Pfont);
Uimanager.put ("Togglebutton.font", Pfont);
Uimanager.put ("Combobox.font", Pfont);
Uimanager.put ("Comboboxitem.font", Pfont);
Uimanager.put ("Internalframe.titlefont", Pfont);
Uimanager.put ("Label.font", Pfont);
Uimanager.put ("List.font", Pfont);
Uimanager.put ("Menubar.font", Pfont);
Uimanager.put ("Menu.font", Pfont);
Uimanager.put ("Menuitem.font", Pfont);
Uimanager.put ("Radiobuttonmenuitem.font", Pfont);
Uimanager.put ("Checkboxmenuitem.font", Pfont);
Uimanager.put ("Popupmenu.font", Pfont);
Uimanager.put ("Optionpane.font", Pfont);
Uimanager.put ("Panel.font", Pfont);
Uimanager.put ("Progressbar.font", Pfont);
Uimanager.put ("Scrollpane.font", Pfont);
Uimanager.put ("Viewport", Pfont);
Uimanager.put ("Tabbedpane.font", Pfont);
Uimanager.put ("Tableheader.font", Pfont);
Uimanager.put ("Table.font", Pfont);
Uimanager.put ("Textfield.font", Pfont);
Uimanager.put ("Passwordfiled.font", Pfont);
Uimanager.put ("Textarea.font", Pfont);
Uimanager.put ("Textpane.font", Pfont);
Uimanager.put ("Editorpane.font", Pfont);
Uimanager.put ("Titledborder.font", Pfont);
Uimanager.put ("Toolbar.font", Pfont);
Uimanager.put ("Tooltip.font", Pfont);
Uimanager.put ("Tree.font", Pfont);
}
Control the character set of the software output by passing a font name to avoid garbled

2. JSP: Can be set CharSet to solve
For example: <%@ page contenttype= "text/html;charset=gb2312"%>

3. Database: Java in the database stream will also produce coding problems, you can refer to the file method, in the receiving stream with a specific character set to decode
For example: Use Method InputStreamReader (InputStream in, Charset CS) to implement




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.