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

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

怎么用native2ascii處理properties文件

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

作者:西西點擊:82次評論:0次標簽: native2ascii

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

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

可native文件中居然有3000多條,如果全手工通過native2asccii來轉換的話,簡直是惡夢。因為我是懶人嘛,當然要用懶辦法。寫了個Java程序調用native2ascii搞定。

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

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

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

(int)a 出來的結果不太對嘛!
你應該對應\u4f60

只是我忽略了進制,要求是16進制,我給的是10進制。

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

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

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

直接打開運行,輸入cmd,就可以轉碼了,如圖

代碼如下:

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)其實上面的做法是脫了褲子放屁,native2ascii本身就能完美的完成這個工作。其詳細用法如下:

native2ascii [options] [inputfile [outputfile]

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

呵呵,不過上面程序也不能說完全沒有用,權當作為一個用java調用操作系統(tǒng)進程的一個練習吧。

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

    相關評論

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

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

    熱門評論

    最新評論

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

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