西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)java → 怎么用native2ascii處理properties文件

怎么用native2ascii處理properties文件

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時(shí)間:2014/2/16 9:56:51字體大。A-A+

作者:西西點(diǎn)擊:82次評論:0次標(biāo)簽: native2ascii

  • 類型:編程輔助大。20KB語言:中文 評分:10.0
  • 標(biāo)簽:
立即下載

Liferay的資源文件做的還是不錯(cuò)的,基本上界面里的所有消息都放到了資源文件中。具體中文的就是:portal-impl/content下面的Language_zh_CN.properties和Language_zh_CN.properties.native。前一個(gè)是unicode字符串,后一個(gè)是中文。先將哪個(gè)native文件內(nèi)容翻譯過來,再把漢字轉(zhuǎn)化為unicode替換前一個(gè)文件就OK了。

可native文件中居然有3000多條,如果全手工通過native2asccii來轉(zhuǎn)換的話,簡直是惡夢。因?yàn)槲沂菓腥寺,?dāng)然要用懶辦法。寫了個(gè)Java程序調(diào)用native2ascii搞定。

native2ascii這個(gè)工具主要用來把本地編碼(比如gbk)的文件轉(zhuǎn)換成標(biāo)準(zhǔn)的Properties屬性文件。
屬性文件中,除字母數(shù)字外的字符要用\轉(zhuǎn)義,具體的標(biāo)準(zhǔn)參考java文檔Properties類的說明。

那這個(gè)轉(zhuǎn)換的原理是什么呢?自己如何實(shí)現(xiàn)呢?

只能概括下原理。
替換掉本地編碼的文本文件中所有的非ascii字符:
比如漢字,先從本地編碼GBK轉(zhuǎn)換成對應(yīng)的unicode字符,
再把這個(gè)字符的字符碼,以\u21342的形式寫回。
char a='你';
 System.out.println(a+" -> \\u"+(int)a);
===========
你 -> \u20320

(int)a 出來的結(jié)果不太對嘛!
你應(yīng)該對應(yīng)\u4f60

只是我忽略了進(jìn)制,要求是16進(jìn)制,我給的是10進(jìn)制。

"原理"是對的
你的基礎(chǔ)差到連一點(diǎn)變通都沒法嗎?
char a='你';
System.out.println(a+" -> \\u"+Integer.toHexString((int)a));
===========
你 -> \u4f60

首先要安裝JDK(不是jre),安裝好后將jdk的bin目錄添加到系統(tǒng)變量path中,然后就可以使用native2ascii命令在控制臺(tái)(cmd)中進(jìn)行轉(zhuǎn)碼了
native2ascii a.properties b.properties
當(dāng)前目錄下要有a.properties這個(gè)文件,如果沒有就要寫全路徑

如果你用eclipse做開發(fā)工具,還是下載一個(gè)propedit的插件吧

直接打開運(yùn)行,輸入cmd,就可以轉(zhuǎn)碼了,如圖

代碼如下:

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Properties;

/**
 * @author smilingleo E-mail:liuwei.dt@gmail.com
 * @version created time:2007-11-2 上午11:37:55 類說明:
 */

public class Native2Ascii {
    static String java_bin_path = "C:/Java/jdk1.5/bin";

    public Native2Ascii() {

    }

    public Properties getProperties(String filename) throws IOException {
        Properties p = new Properties();
        ClassLoader cl = this.getClass().getClassLoader();
        FileInputStream input;
        input = new FileInputStream(filename);
        p.load(input);
        return p;

    }
    public String getUnicodeString(String value) {

        StringBuffer tempSb = new StringBuffer();
        try {
            Process pro = Runtime.getRuntime().exec(
                    java_bin_path + "/native2ascii.exe ");
            OutputStream out = pro.getOutputStream();
            out.write(value.getBytes());
            out.flush();
            out.close();
            InputStreamReader child_in = new InputStreamReader(pro.getInputStream());
            
            int c;
            while ((c = child_in.read()) != -1) {
                tempSb.append((char) c);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return tempSb.toString();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String sourceFile = "Language_zh_CN.properties";
        String targetFile = "target.properties";
        if (args.length != 2) {
            System.out.println("Usage: java Native2Ascii <source properties filename> <target filename>" + 
                    " Author:Smilingleo" + 
                    " Blog:blog.csdn.net/smilingleo");
//            System.exit(0);
        }else{
            sourceFile = args[0];
            targetFile = args[1];
        }
        Native2Ascii parser = new Native2Ascii();
        StringBuffer sb = new StringBuffer();
        try {
            //Convert the source file into unicode first.
            Properties p = parser.getProperties(sourceFile);
            Iterator iterator = p.keySet().iterator();
            while (iterator.hasNext()){
                Object key = iterator.next();
                String value = p.get(key).toString();
                value= new String(value.getBytes("ISO-8859-1"),"UTF-8");
                value = parser.getUnicodeString(value);
//                System.out.println(key + ":" + value);
                p.setProperty(key.toString(), value);
                sb.append(key.toString() + "=" + value);
            }
            //write the target file.
            FileWriter out = new FileWriter(targetFile);
            out.write(sb.toString());
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 ////////////////////////

后來發(fā)現(xiàn)其實(shí)上面的做法是脫了褲子放屁,native2ascii本身就能完美的完成這個(gè)工作。其詳細(xì)用法如下:

native2ascii [options] [inputfile [outputfile]

在options中指定-encoding UTF8就OK了。

呵呵,不過上面程序也不能說完全沒有用,權(quán)當(dāng)作為一個(gè)用java調(diào)用操作系統(tǒng)進(jìn)程的一個(gè)練習(xí)吧。

    轉(zhuǎn)碼工具
    (27)轉(zhuǎn)碼工具
    轉(zhuǎn)碼工具是專門針對把自已不需要的文件格式轉(zhuǎn)換自已需要的轉(zhuǎn)碼大師工具軟件,比如移動(dòng)設(shè)備中只支持某些格式,那就需要使用相關(guān)轉(zhuǎn)碼工具來轉(zhuǎn)換文件來達(dá)到目的了,我們可以轉(zhuǎn)換視頻,圖片,音頻,文檔都是可以轉(zhuǎn)換的,視頻轉(zhuǎn)換工具,轉(zhuǎn)碼工具可以轉(zhuǎn)換所有流行的視頻格式,還能從視頻中獲取音頻,圖片是相互之間的轉(zhuǎn)換處理,使用這些軟件可以很好的幫到我們。...更多>>

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價(jià)!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(0)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)