处理Properties

拼接两个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 //※
        );
    }
    
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值