1.使用Properties IO流方式
import java.io.IOException;
class PropertiesTest {
public static void main(String[] args) throws IOException{
//1.创建properties对象
Properties properties = new Properties();
//2.加载配置文件
//InputStream inputStream = PropertiesTest.class.getResourceAsStream("/resources/config.properties");
//多线程使用
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/config.properties");
properties.load(inputStream);
//3.读取配置信息
System.out.println(properties.getProperty("username"));
System.out.println(properties.getProperty("passwd"));
}
}
2.使用ResourceBundle
import java.util.ResourceBundle;
class ResourceBundleTest {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle("resources/config");//不需要添加文本后缀
System.out.println(bundle.getString("username"));
System.out.println(bundle.getString("passwd"));
}
}