加载properties资源配置文件

一、Properties类加载properties资源

		
		加载src下的配置文件:
				Properties p=new Properties();
				p.load(App.class.getResourceAsStream("/db.properties"));

		
		加载src下的配置文件(配置文件含中文):
				 Properties p=new Properties();
				 p.load(new InputStreamReader(SheetUtils.class.getResourceAsStream("/column.properties")));

1.1 加载src / db.properties
src / db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11
password=root
username=root
maxActive=20
minIdle=3
initialSize=20
App.java
public class App {
	public static void main(String[] args) throws IOException {
		
		//1.使用Properties类读取src下的properties配置文件
		Properties p=new Properties();
		p.load(App.class.getResourceAsStream("/db.properties"));
		
		Enumeration<String> propertyNames = (Enumeration<String>) p.propertyNames();
		
		while(propertyNames.hasMoreElements()) {
			String key = propertyNames.nextElement();
			String value=(String) p.get(key);
				
			System.out.println(key+"="+value);
		}
	}
}

二、ResourceBundle类加载properties资源


	ResourceBundle:
				
				1.加载src/db.properties 配置文件
					ResourceBundle rs=ResourceBundle.getBundle("db");
				
				2.加载src/com/jsoft/demo/db.properties
					ResourceBundle rs=ResourceBundle.getBundle("org.jsoft.demo.db");
	
				3.国际化
		
2.1 加载src/db.properties配置
src / db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11
password=root
username=root
maxActive=20
minIdle=3
initialSize=20
App.java
public class App {
	public static void main(String[] args) throws IOException {
		
		//加载src/db.properties配置
		ResourceBundle rs=ResourceBundle.getBundle("db");
		Enumeration<String> ens = rs.getKeys();
		while(ens.hasMoreElements()) {
			String key = ens.nextElement();
			String value = rs.getString(key);
			System.out.println(key+"="+value);
		}
		
		
	}
}
2.2 加载src/org/jsoft/demo/db.properties配置
src/org/jsoft/demo/db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day11
password=root
username=root
maxActive=20
minIdle=3
initialSize=20
App.java
public class App {
	public static void main(String[] args) throws IOException {
		
		//加载src/db.properties配置
		ResourceBundle rs=ResourceBundle.getBundle("org.jsoft.demo.db");
		Enumeration<String> ens = rs.getKeys();
		while(ens.hasMoreElements()) {
			String key = ens.nextElement();
			String value = rs.getString(key);
			System.out.println(key+"="+value);
		}
		
		
	}
}

三、Java读取Properties配置文件的6种方式汇总

		
		1. this.getClass().getResourceAsStream()
		2.当前类的加载器进行读取this.getClass().getClassLoader().getResourceAsStream()
		3. ClassLoader类的static方法 getSystemResourceAsStream()
		4. Spring中的 ClassPathResource读取
		5. PropertyResourceBundle读取InputStream6.ResourceBundle.getBundle()
		
1. this.getClass().getResourceAsStream()

	//读取配置文件1
	public void readProperties1() throws IOException {
	    //不加/会从当前包进行寻找,加上/会从src开始找
	    InputStream inputStream = this.getClass().getResourceAsStream("/jdbc.properties");
	    Properties properties=new Properties();
	    properties.load(inputStream);
	    System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
	    System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
	    System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
	    System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
	}
	//读取配置文件2        
	 public void readProperties1() throws IOException {
        InputStream inputStream = this.getClass().getResourceAsStream("/config/jdbc2.properties");
        Properties properties=new Properties();
        properties.load(inputStream);
        System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
        System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
        System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
        System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
	}
	
2.当前类的加载器进行读取this.getClass().getClassLoader().getResourceAsStream()

	//读取配置文件1
	public void readProperties2() throws IOException {
	    //不加/,若加了会为null
	    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
	    Properties properties=new Properties();
	    properties.load(inputStream);
	    System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
	    System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
	    System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
	    System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
	}
	//读取配置文件2
	public void readProperties2() throws IOException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/jdbc2.properties");
        Properties properties=new Properties();
        properties.load(inputStream);
        System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
        System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
        System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
        System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
	}

	方法12区别: (classpath即为target/classes 这个目录)
	
	Class.getResourceAsStream() 
				从当前类所在的位置开始查找配置文件位置。
				要找到jdbc.properties和jdbc2.properties必须加/从classpath下开始查找
	
	Class.getClassLoader().getResourceAsStream() 
				默认就从classpath路径下开始查找,加上/会报空指针
	

3. ClassLoader类的static方法 getSystemResourceAsStream()
public void readProperties3() throws IOException {
        //InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties");
        InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");
        Properties properties=new Properties();
        properties.load(inputStream);
        System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
        System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
        System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
        System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
    }
4. Spring中的 ClassPathResource读取
public void readProperties4() throws IOException {
        //ClassPathResource resource = new ClassPathResource("jdbc.properties");
        ClassPathResource resource = new ClassPathResource("config/jdbc2.properties");
        Properties properties= PropertiesLoaderUtils.loadProperties(resource);
        System.out.println("jdbc.driver="+properties.getProperty("jdbc.driver"));
        System.out.println("jdbc.url="+properties.getProperty("jdbc.url"));
        System.out.println("jdbc.username="+properties.getProperty("jdbc.username"));
        System.out.println("jdbc.password="+properties.getProperty("jdbc.password"));
}
5. PropertyResourceBundle读取InputStream流
public void readProperties5() throws IOException {
        //InputStream inputStream = ClassLoader.getSystemResourceAsStream("jdbc.properties");
    	InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/jdbc2.properties");
        PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
        System.out.println(bundle.getString("jdbc.driver"));
        System.out.println(bundle.getString("jdbc.url"));
        System.out.println(bundle.getString("jdbc.username"));
        System.out.println(bundle.getString("jdbc.password"));
    }
6.ResourceBundle.getBundle()
//不用输入后缀
public void readProperties6()  {
        //ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
        ResourceBundle bundle=ResourceBundle.getBundle("config/jdbc2");
        System.out.println(bundle.getString("jdbc.driver"));
        System.out.println(bundle.getString("jdbc.url"));
        System.out.println(bundle.getString("jdbc.username"));
        System.out.println(bundle.getString("jdbc.password"));
    }
7.案例

public class JavaRead {


    @Test
    public  void test1() throws IOException {
        InputStream in = this.getClass().getResourceAsStream("/db.properties");
        Properties p=new Properties();
        p.load(in);

        Enumeration<?> enumeration = p.propertyNames();
        while(enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
    }


    @Test
    public  void test2() throws IOException {
        InputStream in = this.getClass().getClassLoader().getResourceAsStream("db.properties");
        Properties p=new Properties();
        p.load(in);

        Enumeration<?> enumeration = p.propertyNames();
        while(enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
    }



    @Test
    public  void test3() throws IOException {

        InputStream in = ClassLoader.getSystemResourceAsStream("db.properties");

        Properties p=new Properties();
        p.load(in);

        Enumeration<?> enumeration = p.propertyNames();
        while(enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
    }


    @Test
    public  void test4() throws IOException {

        ClassPathResource resource=new ClassPathResource("db.properties");
        Properties p = PropertiesLoaderUtils.loadProperties(resource);

        Enumeration<?> enumeration = p.propertyNames();
        while(enumeration.hasMoreElements()){
            System.out.println(enumeration.nextElement());
        }
    }

    @Test
    public  void test5() throws IOException {

        InputStream in = ClassLoader.getSystemResourceAsStream("db.properties");

        PropertyResourceBundle p = new PropertyResourceBundle(in);

        Enumeration<String> keys = p.getKeys();
        while(keys.hasMoreElements()){
            System.out.println(keys.nextElement());
        }
    }


    @Test
    public  void test6() throws IOException {

        ResourceBundle bundle= ResourceBundle.getBundle("db");

        Enumeration<String> keys = bundle.getKeys();
        while(keys.hasMoreElements()){
            System.out.println(keys.nextElement());
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值