快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

Java 字符编码问题

1.关于文件编码
一般文件保存(文本文件),在保存的时候按系统默认语言编码(字符集)保存, 在中文系统上是GBK,英文系统上是ISO8859_1, 日文系统上是MS932. 在打开的时候同样是按当前系统默认语言(字符集)编码去解码,所以一些中文汉字在日文系统上会显示乱码的原因就是这个. 说白了也就是打开文件时候不同平台使用的字符集不同, 例如中文字符集包括 a,b,c, 日文字符集包括a,b,当在日文系统上解码汉字c的时候,就会出现所谓的乱码。

2. java工作处理编码模式
(1)java在运行期一律以unicode来存储字符,这样有利的支持了多语言环境.
(2)java在读取文件的时候默认是按照系统默认语言(字符集)编码来解码文件,读取和保存时候的编码不一致也导致程序中参数值错误,用FileInputStream类读取文件可以指定编码读取
(3)java在输出到系统界面时(windows)会把内存中变量字符再通过系统默认语言(字符集)编码去转变,所以在输出过程中也会遇到一系列的编码问题

3.解决方案
(1)swing/awt : 输出到系统界面解码时可以通过设置组件显示字体来替换系统字符集解码
例如:
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);
}
通过传递一个字体名称来控制软件输出的字符集,避免产生乱码

2. JSP : 可以通过设置charset来解决
例如:<%@ page contentType="text/html;charset=gb2312" %>

3. 数据库: java在数据库流中也会产生编码问题, 可以参照文件的方式,在接收流时以特定的字符集去解码
例如: 使用方式 InputStreamReader(InputStream in, Charset cs) 可以实现






返回类别: 教程
上一教程: 如何快速成为Java高手[Tomjava原创]
下一教程: EclipsePro: Java Tools for Professional Eclipse Developers

您可以阅读与"Java 字符编码问题"相关的教程:
· JAVA里字符编码的探索与理解
· 关于java中字符编码的一点心得,可能对初学者有点帮助
· JAVA编码问题的一些理解
· JAVA编码问题的一些理解
· Java中文问题详解,底层编码解剖
    微笑服务 优质保证 索取样品