package com.itcast.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**properties读取配置文件
- @author wangqiang
- @version V1.0
- @date 2019/2/16 16:16
*/
public class PropertiesDemo {
public static String username;
public static String password;
static {
//使用静态代码块 当类加载的时候只加载一次
try {
Properties properties = new Properties();
InputStream resourceAsStream = PropertiesDemo.class.getClassLoader().getResourceAsStream(“config.properties”);
properties.load(resourceAsStream);
username= properties.getProperty(“username”);
password= properties.getProperty(“password”);
resourceAsStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(username);
System.out.println(password);
}
}