【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"));
    }
}

 

posted @ 2017-04-14 15:08  Qingyun_Qearl  阅读(362)  评论(0编辑  收藏  举报