利用java.util.Properties读取属性文件
属性文件:properties.properties
username=\u9F99\u94A4
读取:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Prop {
public static void main(String[] s) {
Prop prop = new Prop();
try {
prop.getProp();
} catch (IOException e) {
e.printStackTrace();
}
}
public void getProp() throws IOException {
//使用Class对象的getResourceAsStream()方法,把指定的属性文件读入输入流中
InputStream is = this.getClass().getClassLoader().getResourceAsStream(
"properties.properties");
Properties p = new Properties();
//使用Properties类的load()方法,从输入流中读取属性列表(键/值对)
p.load(is);
//使用Properties类的getProperty()方法,通过键取出value
String username = p.getProperty("username");
System.out.println("username=" + username);
}
}