简体繁体转换

package com.foxconn.ehc.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;

public class Translation {
	
	StringBuffer fantiBuffer; 
	StringBuffer jiantiBuffer; 
	File file; 
	BufferedReader bufferReader; 
	FileReader fileReader; 
	String line; 
	
	public HashMap<Character,Character> fan2Jian = new HashMap<Character,Character>(); 
	public HashMap<Character,Character> jian2Fan = new HashMap<Character,Character>(); 
	
	/** 
	* @param args 
	*/ 
	public Translation(){
		String fantipath = System.getProperty("user.dir")+"\\src\\fanti.txt";
		String jianpath =  System.getProperty("user.dir")+"\\src\\jianti.txt";
		formMap(fantipath,jianpath);
		//System.getProperty("user.dir");
	}; 
	
	public StringBuffer getDictionary(String path) { 
	
		StringBuffer readAll =new StringBuffer(); 
		try{
			file = new File(path); 
			fileReader = new FileReader(file); 
			bufferReader = new BufferedReader(new FileReader(file)); 
			
			while((line = bufferReader.readLine())!=null)
			{ 
				readAll.append(line);
			}
			
		}catch (Exception e){ 
			e.printStackTrace(); 
		} 
			
		return readAll; 
	} 
		
	@SuppressWarnings("unchecked")
	public void formMap(String pathOfFanti,String pathOfJianti){
		jiantiBuffer = getDictionary(pathOfJianti); 
		fantiBuffer = getDictionary(pathOfFanti); 
		int k = jiantiBuffer.length(); 
		char fan ; 
		char jian ;
		
		for(int i=0; i < k; i++){
			fan=fantiBuffer.charAt(i);
			jian=jiantiBuffer.charAt(i);
			
			fan2Jian.put(fan, jian);
			jian2Fan.put(jian, fan);
		}
	}
		
	public String translate(StringBuffer from,String type){ 
		int i = from.length(); 
		char come; 
		if (type.equals("fan2Jian")) {
			for (int k = 0; k < i; k++) {
				come = from.charAt(k);
				if (fan2Jian.containsKey(come)) {
					from.setCharAt(k, fan2Jian.get(come));					
				}
			}
		}else{ 
			for (int k = 0; k < i; k++) { 
				come = from.charAt(k); 
				if (jian2Fan.containsKey(come)) 
				{
					from.setCharAt(k, jian2Fan.get(come));
				}
			}
		}
		
		return from.toString();
	} 
	
	public static void main(String[] args) { 
		Translation tran = new Translation(); 
		StringBuffer from = new StringBuffer(); 
		from.append("核心提示:澳大利亚FAXTS新闻3月5日刊登评论认为,美国在全球一系列被解释成用来防御来自伊朗和朝鲜导弹袭击的弹道导弹防御系统的部署行为,以及最近将先进具有反导能力爱国者导弹出售给台湾的动作,其根本目的是针对两个主要的核大国--中国和俄罗斯。"); 
		String result = tran.translate(from, "jian2Fan"); 
		System.out.println(result);
		
	} 
}

 

 

 

你可能感兴趣的:(转换)