繁简对照表

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.StringTokenizer;

import java.io.InputStreamReader;

import java.util.HashMap;



/**

 * 简体繁体映射表

 * @author dell

 *

 */

public class TCharMap {

    private static TCharMap converter = new TCharMap("ChineseMap.txt");

    

    private HashMap<Character, Character> charMap = new HashMap<Character, Character>(3007);



    /**

     * 

     * @return the singleton of Binary gram dictionary

     */

    public static TCharMap getInstance()

    {

        return converter;

    }

    

    private TCharMap(String dic)

    {

        try{

            InputStream file =  new FileInputStream(new File(dic));            

            BufferedReader in = new BufferedReader(new InputStreamReader(file,"GBK"));            

            String word;

            while ((word = in.readLine()) != null) 

            {                

                StringTokenizer st = new StringTokenizer(word,":" );            

                Character Tword = st.nextToken().charAt(0);

                Character Sword = st.nextToken().charAt(0);

                charMap.put(Tword,Sword);

            }        

            in.close();

        }

        catch(Exception e)

        {

            e.printStackTrace();

        }

    }



    /**

     * 格式化为简体字符串

     * @param simp

     * @return

     */

    public String getSimp(String simp)

    {

        StringBuffer str=new StringBuffer(simp.length());



        for(int i=0;i<simp.length();i++){

            Character s = charMap.get( simp.charAt(i) );

            if( s != null)

                str.append(s);

            else

                str.append(simp.charAt(i));

            

        }

        return str.toString();

    }



    public char getSimp(char simp)

    {

        Character s = charMap.get( simp );

        if( s != null)

            return (s);

        

        return simp;

    }

    public static void main(String[] args) throws Exception {

        TCharMap sc = new TCharMap("ChineseMap.txt");

        String text = sc.getSimp("特首曾蔭權表示,梁展文有權就審計報告提出司法覆核,至於他應否接受立法會政府帳目委員會的聆訊,則是該會主席及委員的決定,但希望梁展文能採取合作態度。昨日深夜,梁展文發表聲明,表示在衡量各種因素後,決定出席立法會聆訊回答提問。我們認為,這是理性務實地處理問題的方法。此外,部分人士在報章登公開信支持梁展文,對此我們認為,既不應因為嘉亨灣事件而抹煞梁展文過去在政府工作中的貢獻,亦不能因梁展文的貢獻而忽視嘉亨灣事件涉及公眾利益,二者不宜混為一談");

        System.out.println(text);

    }

}

 

你可能感兴趣的:(表)