Java 中文问题的根源分析及解决 在简体中文 MS Windows 98 + JDK 1.3 下,可以用 System.getProperties() 得到 Java 运行环境的一些基本属性,类 PoorChinese 可以帮助我们得到这些属性。 类 PoorChinese 的源代码: public class PoorChinese { public static void main(String[] args) { System.getProperties().list(System.out); } } 执行 java PoorChinese 后,我们会得到: 系统变量 file.encoding 的值为 GBK ,user.language 的值为 zh , user.region 的值为 CN ,这些系统变量的值决定了系统默认的编码方式是 GBK 。 在上述系统中,下面的代码将 GB2312 文件转换成 Big5 文件,它们能够帮助我们理解 Java 中汉字编码的转化: import java.io.*; import java.util.*; public class gb2big5 { static int iCharNum=0; public static void main(String[] args) { System.out.println("Input GB2312 file, output Big5 file."); if (args.length!=2) { System.err.println("Usage: jview gb2big5 gbfile big5file"); System.exit(1); } String inputString = readInput(args[0]); writeOutput(inputString,args[1]); System.out.println("Number of Characters in file: "+iCharNum+"."); } static void writeOutput(String str, String strOutFile) { try { FileOutputStream fos = new FileOutputStream(strOutFile); Writer out = new OutputStreamWriter(fos, "Big5"); out.write(str); out.close(); } catch (IOException e) { e.printStackTrace(); e.printStackTrace(); } } static String readInput(String strInFile) { StringBuffer buffer = new StringBuffer(); try { FileInputStream fis = new FileInputStream(strInFile); InputStreamReader isr = new InputStreamReader(fis, "GB2312"); Reader in = new BufferedReader(isr); int ch; while ((ch = in.read()) > -1) { iCharNum += 1; buffer.append((char)ch); } in.close(); return buffer.toString(); } catch (IOException e) { e.printStackTrace(); return null; } } } 编码转化的过程如下: ByteToCharGB2312 CharToByteBig5 GB2312------------------>Unicode------------->Big5
试试反转可以不
试试反转可以不
解决方案 »
- java编程 求一个负整数的二进制数
- 请问离散数学为什么叫离散数学?
- 角谷猜想的证明
- 求Java编程思想 (第四版)用到的软件包 (net.mindview.util.Print.*)
- 有2个简单的问题,想确定下
- m10和dev有什么区别?
- 急啊~~我们的课程设计不知道怎么弄啊 请教各位啦~~感谢啊啊
- 急急急,全局变量传参问题(附精简源码)
- 在java应用程序中,如果在屏幕任意处显示有颜色的点?
- solaris下写文件操作的中文问题
- 请问:有没有方法可以在不移动ResultSet的情况判断查询的记录数是否为0,也就是说什么都没查出来!
- 哪有JAVA例程下载?尤其是for Solaris的!!!
thanks