www.pudn.com > charsettools_1.0.2.zip > GB2BIG5.java
/*Program GB2BIG.java *
Author Keiven Ju *
Created 2003-5-15 5:42:41 *
Copyright 1995-1998,2000-2003 by (rc) AKuP International, Inc.,
*
Nanjing:
* Unit C, 26F Jianjiang Culture Building, No. 89 Zhongshannan Rd. Nanjing 210005 China
*
Taipei :
* 4F, 213 Sec.5, Nanking East Rd., Taipei, Taiwan. (105).
*
* All rights reserved.
*
* This software is the confidential and proprietary information
* of AKuP International, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with AKuP.
*
* E-mail keiven.ju@akup.com for more information. */ package com.akup.charset; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringReader; import java.io.Writer; /** * GB2BIG5 * ±àÂ?ª»¯µÄ¹?ÌÈçÏ£º * GB2312------------------>Unicode------------->Big5 * @author Keiven Ju (keiven.ju@akup.com) * @since 1.0.0 * @version 1.0.1 2003-5-15 */ public class GB2BIG5 { private static int iCharNum = 0; /** * Method readInput ´ÓÎļ?жÁ³ö×Ö·û´® * @param strInFile * @return String */ public 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; } } /** * Method writeOutput ×Ö·û´®Êä³öΪBig5±àÂ?ļ?£ * @param str ´?ä³ö×Ö·û´® * @param strOutFile Êä³öÎļ? */ public 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(); } } /** * Method readInput ´ÓÎļ?жÁ³ö×Ö·û´® * @param strInFile * @return String */ public static String readInput(String strInFile, String charset) { if (charset == null) return null; StringBuffer buffer = new StringBuffer(); try { FileInputStream fis = new FileInputStream(strInFile); InputStreamReader isr = new InputStreamReader(fis, charset); 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; } /** * writeOutput,×Ö·û´®Êä³öΪtrgcharset±àÂ?ļ?£ * @param str ´?ä³ö×Ö·û´® * @param strOutFile Êä³öÎļ? * @param trgcharset Êä³öÎļ?harset * @since 1.0.0 2003-12-15 */ public static void writeOutput(String str, String strOutFile, String trgcharset) { try { FileOutputStream fos = new FileOutputStream(strOutFile); Writer out = new OutputStreamWriter(fos, trgcharset); out.write(str); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Input GB2312 file, output Big5 file. * @deprecated * @param strInFile * @param strOutFile */ public static void FileGB2BIG(String strInFile, String strOutFile) { iCharNum = 0; System.out.println("Input GB2312 file, output Big5 file."); String inputString = readInput(strInFile); writeOutput(inputString, strOutFile); System.out.println("Number of Characters in file: " + iCharNum + "."); } /** * toIEUnicode, * @param instring * @return String * @since 1.0.0 2003-12-11 */ public static String toIEUnicode(String instring) { String IEUnicode = ""; char IEUnicoded = ';'; StringBuffer out = new StringBuffer(); try { Reader in = new StringReader(instring); String temp = null; int ch; while ((ch = in.read()) > -1) { iCharNum += 1; if (ch < 512) { out.append((char) ch); } else { //temp = IEUnicode + Integer.toHexString(ch)+";"; out.append(IEUnicode); out.append(Integer.toHexString(ch)); out.append(IEUnicoded); } } in.close(); } catch (IOException e) { e.printStackTrace(); return null; } return out.toString(); } /** * File2IEUnicode, * @param infile * @param outfile * @since 1.0.0 2003-12-29 */ public static void File2IEUnicode(String infile, String outfile) { System.out.println("Input GB2312 file, output Unicode file."); String str = readInput(infile); String strunicode = toIEUnicode(str); writeOutput(strunicode, outfile); System.out.println("Number of Characters in file: " + iCharNum + "."); } /** * ÊäÈ?Ö·û´®Êä³?UnicodeÏÔʾ×Ö·û´®¡£ * Method String2Unicode * @param instring * @return String */ public static String String2Unicode(String instring) { String unicode = "\\u"; StringBuffer out = new StringBuffer(); try { Reader in = new StringReader(instring); String temp = null; int ch; while ((ch = in.read()) > -1) { iCharNum += 1; if (ch < 512) { out.append((char) ch); } else { temp = unicode + Integer.toHexString(ch); out.append(temp); } } in.close(); } catch (IOException e) { e.printStackTrace(); return null; } return out.toString(); } /**Input GB2312 file, output Unicode file. * @param infile * @param outfile */ public static void File2Ascii(String infile, String outfile) { System.out.println("Input GB2312 file, output Unicode file."); String str = readInput(infile); String strunicode = String2Unicode(str); writeOutput(strunicode, outfile); System.out.println("Number of Characters in file: " + iCharNum + "."); } public static void main(String[] args) { //FileGB2BIG("AA.txt", "BB.txt"); //String ss = String2Unicode("123ÎÒÊÇÀÏ»¢456"); //File2Ascii("AA.txt", "BB.txt"); //ss=readInput("AA.txt"); System.out.println(String2Unicode("‚킽‚µ“c’†")); //File2IEUnicode("AA.txt", "CC.txt"); } }