程序如下:
import java.util.ResourceBundle;
import java.util.Enumeration;
public class PropertyFile {
public PropertyFile() {
}
public void readPropertyFile() {
ResourceBundle bundle =
ResourceBundle.getBundle("f:/pro/test.properties");
// Enumerate contents of resource bundle
//The next two lines should be in one line.
for (Enumeration props = bundle.getKeys();
props.hasMoreElements(); ) {
String key = (String) props.nextElement();
process(key, bundle.getObject(key));
}
}
public void process(String pKey,Object pValue){
System.out.println("the key = "+pKey+"; the value = "+pKey.toString());
}
public static void main(String[] args){
PropertyFile test = new PropertyFile();
test.readPropertyFile();
}
}
出错:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name f:/pro/test, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:837)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:806)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:550)
at fileOperation.PropertyFile.readPropertyFile(PropertyFile.java:10)
at fileOperation.PropertyFile.main(PropertyFile.java:27)
1:ResourceBundle.getBundle 不用写.properties扩展名,只写了文件名,把文件放在根目录下就可以
2:如果写路径,把/换成转义符 //
ResourceBundle.getBundle("f://pro//test.properties");