现在属性文件在系统中的应用日益广泛,在JAVA中读取属性文件我一般用到的有两种:
1 Properties类直接读取属性文件
public class Test {
public static void main(String[] args) {
InputStream is = Test.class.getResourceAsStream("nojar.properties");
if (is == null)
System.out.println("文件找不到");
Properties prop = new Properties();
try {
prop.load(is);
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(prop.getProperty("test"));
}
}
2 通过ResourceBundle读取属性文件
public class Test {
public static void main(String[] args) {
ResourceBundle rb = ResourceBundle.getBundle("test");
System.out.println(rb.getString("test"));
}
}