2016-08-23 83 views
1

我想在java中运行下面的示例代码。java中的属性文件

import java.util.Locale; 
import java.util.ResourceBundle; 

public class InternationalizationDemo { 

    public static void main(String[] args) {   
    ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH); 
    System.out.println("Message in "+Locale.CANADA_FRENCH +":"+bundle.getString("greeting")); 
    } 
} 

1.上述代码在MessageBundle.properties放置在类路径中时正确执行。 但我想通过从类路径中删除MessageBundle.properties并将它放置在其他位置成功执行上述代码。 我该怎么做? 在此先感谢。

+0

可能的重复:http://stackoverflow.com/questions/3598240/can-we-specify-directory-path-with-the-property-file-while-using-resourcebundle –

+0

不,我不想将属性文件放在任何包中。 – vinayaka

+0

我希望它的任何驱动器像c:\或D:\驱动器 – vinayaka

回答

1

你可以由此外部加载属性文件:

// Path to your file, if you have it on local, use something like C:\\MyFolder or \home\usr\etc 
File file = new File("YOUR_PATH"); 
URL[] url = {file.toURI().toURL()}; 
ResourceBundle rb = ResourceBundle.getBundle("MessageBundle", Locale.CANADA_FRENCH, new URLClassLoader(url)); 

示例来自这里:https://coderanch.com/t/432762/java/java/absolute-path-bundle-file

您可以使用远程属性文件或保存在本地的文件。

+0

现在正在工作。Tkykyou – vinayaka

1

你可以使用PropertyResourceBundle,让你的属性的路径从System属性文件,例如类似的东西:

String configPath = System.getProperty("config.path"); 
ResourceBundle bundle = new PropertyResourceBundle(new FileReader(configPath)); 

然后在你的启动命令,你将需要添加-Dconfig.path=/path/to/my/config.properties