/**
* 繁体和简体的转换
*
*/
public class TrandtionalToSimplified {
private static String encoding = "utf-8";
private static String tra;
private static String sim;
/**
* 初始化繁体和简体字库
*/
public static void initChars() {
String trapath = TrandtionalToSimplified.class.getResource(
"tranditional.txt").getPath();
File trafile = new File(trapath);
String simpath = TrandtionalToSimplified.class.getResource(
"simplified.txt").getPath();
File simfile = new File(simpath);
try {
tra = FileUtils.readFileToString(trafile, encoding);
sim = FileUtils.readFileToString(simfile, encoding);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 简体转繁体
*
* @param simplified
* @return
*/
public static String simTotra(String simplified) {
if (sim == null) {
initChars();
}
String traditional = "";
for (int i = 0; i < simplified.length(); i++) {
if (sim.indexOf(simplified.charAt(i)) != -1) {
traditional += tra.charAt(sim.indexOf(simplified.charAt(i)));
} else {
traditional += traditional.charAt(i);
}
}
return traditional;
}
/**
* 繁体转简体
*
* @param traditional
* @return
*/
public static String traTosim(String traditional) {
if (tra == null) {
initChars();
}
String simplified = "";
for (int i = 0; i < traditional.length(); i++) {
if (tra.indexOf(traditional.charAt(i)) != -1) {
simplified += sim.charAt(tra.indexOf(traditional.charAt(i)));
} else {
simplified += traditional.charAt(i);
}
}
return simplified;
}
/**
* 判断是否是繁体
*
* @param str
* @return
*/
public static boolean isTrandtional(String str) {
if (null == str)
return false;
if (str.trim() == " ")
return false;
byte[] bytes = str.getBytes();
if (bytes.length < 2)
return false;
byte aa = (byte) 0xB0;
byte bb = (byte) 0xF7;
byte cc = (byte) 0xA1;
byte dd = (byte) 0xFE;
if (bytes[0] >= aa && bytes[0] <= bb) {
if (bytes[1] < cc || bytes[1] > dd) {
return false;
}
return true;
}
return false;
}
/**
* 判断是否是简体
*
* @param str
* @return
*/
public static boolean isSimplified(String str) {
if (null == str)
return false;
if (str.trim() == " ")
return false;
byte[] bytes = str.getBytes();
if (bytes.length < 2)
return false;
byte aa = (byte) 0xB0;
byte bb = (byte) 0xF7;
byte cc = (byte) 0xA1;
byte dd = (byte) 0xFE;
if (bytes[0] >= aa && bytes[0] <= bb) {
if (bytes[1] < cc || bytes[1] > dd) {
return true;
}
return false;
}
return false;
}