拼接两个Properties文件
一个英文一个中文翻译
中文转码为Unicode
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class ParseProperties
{
public static void main(String args[]) throws FileNotFoundException,
IOException
{
Properties resource = new Properties();
Properties translate = new Properties();
resource.load(new FileInputStream(new File(
"./resource/resource.properties")));
translate.load(new FileInputStream(new File(
"./resource/translate2.properties")));
BufferedReader reader = new BufferedReader(new FileReader(
"./resource/resource.properties"));
BufferedWriter writer = new BufferedWriter(new FileWriter(
"./resource/target.properties"));
BufferedReader reader2 = new BufferedReader(new FileReader(
"./resource/translate2.properties"));
Map<String, String> map = new HashMap<String, String>();
String tempStr;
while ((tempStr = reader2.readLine()) != null)
{
if (tempStr.contains("="))
{
String key = tempStr.substring(0, tempStr.indexOf("=")).trim();
String value = tempStr.replace(tempStr.substring(0,
tempStr.indexOf("=") + 1),
"").trim();
System.out.println(toUnicode(value));
map.put(key, toUnicode(value));
}
}
while (null != (tempStr = reader.readLine()))
{
if (tempStr.contains("="))
{
String key = tempStr.substring(0, tempStr.indexOf("=")).trim();
tempStr = " " + key + " = en-us#"
+ resource.getProperty(key) + "@zh-cn#" + map.get(key);
}
writer.write(tempStr);
writer.newLine();
}
writer.flush();
writer.close();
}
public static String toUnicode(String str)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if (isSpeChar_zh_cn(c))
{
sb.append("\\u");
sb.append(Integer.toHexString(str.charAt(i)));
}
else
{
sb.append(c);
}
}
return sb.toString();
}
/**
* 判断指定的实体是否属于中文
*/
private static boolean isSpeChar_zh_cn(int nEntity)
{
return ((nEntity >= 0x4E00 && nEntity <= 0x9FA5)
|| (nEntity >= 0xFF01 && nEntity <= 0xFF5E)
|| (nEntity >= 0xFF61 && nEntity <= 0xFF9F)
|| (nEntity >= 0xFFE0 && nEntity <= 0xFFE6)
|| (nEntity >= 0xFFE8 && nEntity <= 0xFFEE)
|| (nEntity >= 0x3000 && nEntity <= 0x3029)
|| (nEntity >= 0x3030 && nEntity <= 0x3037) || nEntity == 8220 //全角左双引号
|| nEntity == 8221 //全角右双引号
|| nEntity == 8216 //全角左单引号
|| nEntity == 8217 //全角右单引号
|| nEntity == 9678 //◎
|| nEntity == 8230 //…
|| nEntity == 8251 //※
);
}
}