本文提供一个java实现中文字符繁简体互换的zip包以及主要的源代码实现说明。
繁简体(GB<=>Big5)中文字符的转化实现原理很简单,就是根据两种码表的编码规则,创建两者之间的字符对应关系表,通过程序读取这个映射表来自动查出另一种编码方式下对应字符的字节编码,从而进行逐字节的内容替换。
主功能实现的GB2Big5.java源代码如下: 已经加入里系统支持的一个类库的源代码,这个是完整版。
- package net.java2000.tools;
- import java.io.BufferedOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- /**
- * 用来处理GB2312/BIG5码字符互相转换的类.<br>
- * 需要两个码表文件: gb-big5.table,/zeal/util/big5-gb.table.<br>
- * 这两个码表可以根据具体情况补充映射不正确的码.
- */
- public class GB2Big5 {
- private static GB2Big5 pInstance = null;
- private String s_big5TableFile = null;
- private String s_gbTableFile = null;
- private byte[] b_big5Table = null;
- private byte[] b_gbTable = null;
- /** 指定两个码表文件来进行初始化 */
- private GB2Big5(String sgbTableFile, String sbig5TableFile) throws NullPointerException {
- s_big5TableFile = sbig5TableFile;
- s_gbTableFile = sgbTableFile;
- if (null == b_gbTable) {
- b_gbTable = getBytesFromFile(sgbTableFile);
- }
- if (null == b_big5Table) {
- b_big5Table = getBytesFromFile(sbig5TableFile);
- }
- if (null == b_gbTable) {
- throw new NullPointerException("No gb table can be load");
- }
- if (null == b_big5Table) {
- throw new NullPointerException("No big5 table can be load");
- }
- }
- public static synchronized GB2Big5 getInstance() {
- // return getInstance("d://gb-big5.table","d://big5-gb.table");
- return getInstance("/net/java2000/tools/gb-big5.table", "/net/java2000/tools/big5-gb.table");
- }
- public static synchronized GB2Big5 getInstance(String sgbTableFile, String sbig5TableFile) {
- if (null == pInstance) {
- try {
- pInstance = new GB2Big5(sgbTableFile, sbig5TableFile);
- } catch (Exception e) {
- System.err.println(e.toString());
- pInstance = null;
- }
- }
- return pInstance;
- }
- /**
- * 把gbChar对应的big5字符替换掉,用来更新码表文件. 一般当发现字符映射不正确的时候可以通过这个方法来校正.
- */
- protected synchronized void resetBig5Char(String gbChar, String big5Char) throws Exception {
- byte[] Text = new String(gbChar.getBytes(), "GBK").getBytes("GBK");
- byte[] TextBig5 = new String(big5Char.getBytes(), "BIG5").getBytes("BIG5");
- int max = Text.length - 1;
- int h = 0;
- int l = 0;
- int p = 0;
- int b = 256;
- for (int i = 0; i < max; i++) {
- h = (int) (Text[i]);
- if (h < 0) {
- h = b + h;
- l = (int) (Text[i + 1]);
- if (l < 0) {
- l = b + (int) (Text[i + 1]);
- }
- if (h == 161 && l == 64) {
- ; // do nothing
- } else {
- p = (h - 160) * 510 + (l - 1) * 2;
- b_gbTable[p] = TextBig5[i];
- b_gbTable[p + 1] = TextBig5[i + 1];
- }
- i++;
- }
- }
- BufferedOutputStream pWriter = new BufferedOutputStream(new FileOutputStream(s_gbTableFile));
- pWriter.write(b_gbTable, 0, b_gbTable.length);
- pWriter.close();
- }
- /**
- * 把big5Char对应的gb字符替换掉,用来更新码表文件. 一般当发现字符映射不正确的时候可以通过这个方法来校正.
- */
- protected synchronized void resetGbChar(String big5Char, String gbChar) throws Exception {
- byte[] TextGb = new String(gbChar.getBytes(), "GBK").getBytes("GBK");
- byte[] Text = new String(big5Char.getBytes(), "BIG5").getBytes("BIG5");
- int max = Text.length - 1;
- int h = 0;
- int l = 0;
- int p = 0;
- int b = 256;
- for (int i = 0; i < max; i++) {
- h = (int) (Text[i]);
- if (h < 0) {
- h = b + h;
- l = (int) (Text[i + 1]);
- if (l < 0) {
- l = b + (int) (Text[i + 1]);
- }
- if (h == 161 && l == 64) {
- ; // do nothing
- } else {
- p = (h - 160) * 510 + (l - 1) * 2;
- b_big5Table[p] = TextGb[i];
- b_big5Table[p + 1] = TextGb[i + 1];
- }
- i++;
- }
- }
- BufferedOutputStream pWriter = new BufferedOutputStream(new FileOutputStream(s_big5TableFile));
- pWriter.write(b_big5Table, 0, b_big5Table.length);
- pWriter.close();
- }
- /** 把gb2312编码的字符串转化成big5码的字节流 */
- public byte[] gb2big5(String inStr) throws Exception {
- if (null == inStr || inStr.length() <= 0) {
- return "".getBytes();
- // return "";
- }
- byte[] Text = new String(inStr.getBytes(), "GBK").getBytes("GBK");
- int max = Text.length - 1;
- int h = 0;
- int l = 0;
- int p = 0;
- int b = 256;
- byte[] big = new byte[2];
- for (int i = 0; i < max; i++) {
- h = (int) (Text[i]);
- if (h < 0) {
- h = b + h;
- l = (int) (Text[i + 1]);
- if (l < 0) {
- l = b + (int) (Text[i + 1]);
- }
- if (h == 161 && l == 64) {
- big[0] = big[1] = (byte) (161 - b);
- } else {
- p = (h - 160) * 510 + (l - 1) * 2;
- try {
- big[0] = (byte) (b_gbTable[p] - b);
- } catch (Exception e) {
- big[0] = 45;
- }
- try {
- big[1] = (byte) (b_gbTable[p + 1] - b);
- } catch (Exception e) {
- big[1] = 45;
- }
- }
- Text[i] = big[0];
- Text[i + 1] = big[1];
- i++;
- }
- }
- return Text;
- // return new String(Text);
- }
- /** 把big5码的字符串转化成gb2312码的字符串 */
- public String big52gb(String inStr) throws Exception {
- if (null == inStr || inStr.length() <= 0) {
- return "";
- }
- byte[] Text = new String(inStr.getBytes(), "BIG5").getBytes("BIG5");
- int max = Text.length - 1;
- int h = 0;
- int l = 0;
- int p = 0;
- int b = 256;
- byte[] big = new byte[2];
- for (int i = 0; i < max; i++) {
- h = (int) (Text[i]);
- if (h < 0) {
- h = b + h;
- l = (int) (Text[i + 1]);
- if (l < 0) {
- l = b + (int) (Text[i + 1]);
- }
- if (h == 161 && l == 161) {
- big[0] = (byte) (161 - b);
- big[1] = (byte) (64 - b);
- } else {
- p = (h - 160) * 510 + (l - 1) * 2;
- try {
- big[0] = (byte) (b_big5Table[p] - b);
- } catch (Exception e) {
- big[0] = 45;
- }
- try {
- big[1] = (byte) (b_big5Table[p + 1] - b);
- } catch (Exception e) {
- big[1] = 45;
- }
- }
- Text[i] = big[0];
- Text[i + 1] = big[1];
- i++;
- }
- }
- return new String(Text);
- }
- /** 把文件读入字节数组,读取失败则返回null */
- private static byte[] getBytesFromFile(String inFileName) {
- try {
- InputStream in = GB2Big5.class.getResourceAsStream(inFileName);
- byte[] sContent = StreamConverter.toByteArray(in);
- in.close();
- return sContent;
- /*
- * java.io.RandomAccessFile inStream = new java.io.RandomAccessFile(inFileName,"r"); byte[] sContent = new byte[ (int)
- * (inStream.length())]; inStream.read(sContent); inStream.close(); return sContent;
- */
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- public static void main(String[] args) throws Exception {
- if (args.length < 2) {
- System.out.println("Usage: net.java2000.tools.GB2Big5 [-gb | -big5] inputstring");
- System.exit(1);
- return;
- }
- boolean bIsGB = true;
- String inStr = "";
- for (int i = 0; i < args.length; i++) {
- if (args[i].equalsIgnoreCase("-gb")) {
- bIsGB = true;
- } else if (args[i].equalsIgnoreCase("-big5")) {
- bIsGB = false;
- } else {
- inStr = args[i];
- }
- }
- GB2Big5 pTmp = GB2Big5.getInstance();
- String outStr = "";
- if (bIsGB) {
- outStr = pTmp.big52gb(inStr);
- } else {
- outStr = new String(pTmp.gb2big5(inStr), "BIG5");
- }
- System.out.println("String [" + inStr + "] converted into:/n[" + outStr + "]");
- }
- }
- class StreamConverter {
- public StreamConverter() {
- }
- public static byte[] toByteArray(InputStream input) throws IOException {
- int status = 0;
- int totalBytesRead = 0;
- int blockCount = 1;
- byte dynamicBuffer[] = new byte[5000 * blockCount];
- byte buffer[] = new byte[5000];
- boolean endOfStream = false;
- do {
- if (endOfStream)
- break;
- int bytesRead = 0;
- if (input.available() != 0) {
- status = input.read(buffer);
- endOfStream = status == -1;
- if (!endOfStream)
- bytesRead = status;
- } else {
- status = input.read();
- endOfStream = status == -1;
- buffer[0] = (byte) status;
- if (!endOfStream)
- bytesRead = 1;
- }
- if (!endOfStream) {
- if (totalBytesRead + bytesRead > 5000 * blockCount) {
- blockCount++;
- byte newBuffer[] = new byte[5000 * blockCount];
- System.arraycopy(dynamicBuffer, 0, newBuffer, 0, totalBytesRead);
- dynamicBuffer = newBuffer;
- }
- System.arraycopy(buffer, 0, dynamicBuffer, totalBytesRead, bytesRead);
- totalBytesRead += bytesRead;
- }
- } while (true);
- byte result[] = new byte[totalBytesRead];
- if (totalBytesRead != 0)
- System.arraycopy(dynamicBuffer, 0, result, 0, totalBytesRead);
- return result;
- }
- }
由于这里不能上传附件,所需要的2个字体对照表,请到这里下载,或者到原作者的网页下载。那里还有完整的项目包
http://www.java2000.net/p8902
<script type="text/javascript"><!--google_ad_client = "pub-2908059660288034";/* 728x90,首页中间 创建于 08-8-14 */google_ad_slot = "5903610560";google_ad_width = 728;google_ad_height = 90;//--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>