【Java基础】使用类加载器 加载properties文件
import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class LoadResourcesDemo { public static void main(String[] args) throws Exception { //获取加载资源的文件名 String resourceName = "db.properties"; Properties properties = new Properties(); //使用类加载器 ClassLoader 加载classPath的根路径 ClassLoader loader = Thread.currentThread().getContextClassLoader(); //读入数据流 InputStream InputStream = loader.getResourceAsStream(resourceName); properties.load(InputStream); System.out.println(properties.getProperty("username")); System.out.println(properties.getProperty("password")); } }
如果错过了一天,那么真的就错过一天。不抛弃,不放弃。点一盏心灯给自己。