2009-01-05 11:52:29softlive

使用java的ResourceBundle(基本用法)


AsmPosition.properties(Properties檔名。以下為內容。這個檔必須跟執行檔同一路徑)
# This is the file position of the template
# Please put this file in the same path with the jar file
Name=SampleFile.txt
Path=D:\work\PCG\dist  <--- 路徑須兩個''符號


Compile.java(測試主檔)
package com.softlive;

import java.util.ResourceBundle;

public class Compile {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {        
        ResourceBundle resource = ResourceBundle.getBundle("AsmPosition");   
        //只要寫'AsmPosition'就好,不用寫'.properties'
        System.out.println("-----------------------------");
        System.out.println(resource.getString("Name"));
        System.out.println(resource.getString("Path"));
        System.out.println("-----------------------------");
    }

}

以下是使用NetBeans 6.5執行輸出(PCG是我的測試project name,.dist是執行時NetBeans產生jar檔位置,AsmPosition.properties必須在這個位置。)
run:
-----------------------------
PointsCollector.asm
D:workPCGdist
-----------------------------
BUILD SUCCESSFUL (total time: 0 seconds)

如此一來,就可以修改路徑名稱,不用再重新編譯程式。