各位好: 我想问一个问题,我们平时需要属性配置文件时,都在SRC下建立,然后加载ResourceBundle.getBundle("db");
能不能指定properties到其他路径下呢? java 中有没有提供像ResourceBundle.getBundle("路径+name");
这样的呢。 其实意思就是想不要将属性文件放在src下面。

解决方案 »

  1.   

    采用.的方式import java.awt.Color;
    import java.text.DateFormat;
    import java.util.Date;
    import java.util.Locale;
    import java.util.ResourceBundle;import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JTextArea;//使用程序国际化使用资源包的方法------资源绑定类------掌握
    public class TestResourceBundle {
    private static ResourceBundle bundle; private static Locale currentLocale;
    static {
    currentLocale = Locale.getDefault();
                    //在src目录下(可以自己制定目录)建Properties包,MyLabels一些文件在Properties目录中。
    bundle = ResourceBundle.getBundle("Properties.MyLabels", currentLocale);
    } public static void main(String[] args) {
    JFrame jf = new JFrame(bundle.getString("title"));
    JMenuBar jmb = new JMenuBar();
    JMenu jm1 = new JMenu(bundle.getString("menu.file"));
    JMenu jm2 = new JMenu(bundle.getString("menu.edit"));
    JMenu jm3 = new JMenu(bundle.getString("menu.help"));
    jmb.add(jm1);
    jmb.add(jm2);
    jmb.add(jm3);
    jf.setJMenuBar(jmb);
    JTextArea status = new JTextArea();
    status.setText(bundle.getString("currentEnv")
    + currentLocale.getDisplayName());
    // 从此资源包或它的某个父包中获取给定键的对象
    Color bg = (Color) bundle.getObject("bgColor");
    status.setBackground(bg);
    JLabel time = new JLabel();
    jf.add(status, "Center");
    jf.add(time, "South");
    jf.setSize(350, 200);
    jf.setLocation(400, 300);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setVisible(true); MyTimer1 mt = new MyTimer1(time);
    mt.start();
    }
    }class MyTimer1 extends Thread {
    JLabel time; public MyTimer1(JLabel time) {
    this.time = time;
    } public void run() {
    Locale locale = Locale.getDefault();
    while (true) {
    Date now = new Date();
    // 获取日期/时间格式器,该格式器具有给定语言环境的给定格式化风格。
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
    DateFormat.FULL, locale);
    time.setText(df.format(now));
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }Properties中的一类资源文件,如下:
    MyLabels_zh_CN.java
    MyLabels_zh_TW.java
    MyLabels.java
    不知是否符合???
      

  2.   

    使用URL url = this.getClass().getResource(propertyFileName);方式也可以。