Java中⽂简体繁体转换
项⽬路径如下
使⽤⽅法如下:
public class Translation {
public static void main(String[] args) {
ZHConverter converter = Instance(ZHConverter.SIMPLIFIED);
//繁体转换简体
String traditionalSrc = "核⼼提⽰:澳⼤利亞FAXTS新聞3⽉5⽇刊登評論認為,美國在全球⼀系列被解釋成⽤來防禦來⾃伊朗和朝鮮導彈襲擊的彈道導 String simplified = vert(traditionalSrc);
System.out.println(simplified);
//简体转换繁体
String traditional = vert(simplified,ZHConverter.TRADITIONAL);
System.out.println(traditional);
}
}大s和小s是双胞胎吗
附上ZHConverter代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
qq的网名import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
iphone13和13proimport java.util.Set;
public class ZHConverter {
private Properties charMap = new Properties();
private Set<String> conflictingSets = new HashSet<String>();
public static final int TRADITIONAL = 0;
public static final int SIMPLIFIED = 1;
private static final int NUM_OF_CONVERTERS = 2;
private static final ZHConverter[] converters = new ZHConverter[NUM_OF_CONVERTERS];
private static final String[] propertyFiles = new String[2];
static {
propertyFiles[TRADITIONAL] = "zh2Hant.properties"; //附件下载
propertyFiles[SIMPLIFIED] = "zh2Hans.properties"; //附件下载
陈冠希门女星名单}
/**
*
* @param converterType 0 for traditional and 1 for simplified
* @return
*/
public static ZHConverter getInstance(int converterType) {
if (converterType >= 0 && converterType < NUM_OF_CONVERTERS) {
if (converters[converterType] == null) {
synchronized(ZHConverter.class) {
synchronized(ZHConverter.class) {
if (converters[converterType] == null) {
converters[converterType] = new ZHConverter(propertyFiles[converterType]); }
}
}
return converters[converterType];
} else {
return null;
}
}
public static String convert(String text, int converterType) {
ZHConverter instance = getInstance(converterType);
vert(text);
}
private ZHConverter(String propertyFile) {
InputStream is = null;
is = getClass().getResourceAsStream(propertyFile);
//File propertyFile = new File("C:/Temp/testMDB/");
if (is != null) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(is));
吴秀波演过红孩儿吗charMap.load(reader);
} catch (FileNotFoundException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
if (is != null)
is.close();
} catch (IOException e) {
}
}
}
initializeHelper();
}
@SuppressWarnings("rawtypes")
private void initializeHelper() {
Map<String,Integer> stringPossibilities = new HashMap<String,Integer>();
Iterator iter = charMap.keySet().iterator();
while (iter.hasNext()) {
String key = (String) ();
if (key.length() >= 1) {
for (int i = 0; i < (key.length()); i++) {
String keySubstring = key.substring(0, i + 1);
if (ainsKey(keySubstring)) {
Integer integer = (Integer)((keySubstring));
stringPossibilities.put(keySubstring, new Integer(
integer.intValue() + 1));
} else {
stringPossibilities.put(keySubstring, new Integer(1));
}
}
}
}
iter = stringPossibilities.keySet().iterator();
while (iter.hasNext()) {
String key = (String) ();
String key = (String) ();
if (((Integer)((key))).intValue() > 1) {
conflictingSets.add(key);
}
}
}
public String convert(String in) {
StringBuilder outString = new StringBuilder();
StringBuilder stackString = new StringBuilder();
for (int i = 0; i < in.length(); i++) {
素食主义
char c = in.charAt(i);
String key = "" + c;
stackString.append(key);
if (String())) {
} else if (String())) {
outString.(String()));
stackString.setLength(0);
} else {
CharSequence sequence = stackString.subSequence(0, stackString.length()-1); stackString.delete(0, stackString.length()-1);
flushStack(outString, new StringBuilder(sequence));
}
}
flushStack(outString, stackString);
String();
private void flushStack(StringBuilder outString, StringBuilder stackString) {
while (stackString.length() > 0){
if (String())) {
outString.(String()));
stackString.setLength(0);
} else {
outString.append("" + stackString.charAt(0));
stackString.delete(0, 1);
}
}
}
String parseOneChar(String c) {
if (ainsKey(c)) {
return (String) (c);
}
return c;
}
}
发布评论