Map实现类Properties

Properties属于Map接口中Hashtable实现类

存取数据时,建议使用setProperty(String key,String value)方法和getProperty(String key)方法。

datasource.properties文件:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF8
username=root
password=7777

取数据

public class AppTest {
    private Logger logger = Logger.getLogger(this.getClass());
        @Test
    public void testProperties(){
        Properties pros = new Properties();
        try {
//            pros.load(new FileInputStream("D:\\code\\Acoffee-Spring\\spring-02-di\\src\\main\\resources\\datasource.properties"));
            pros.load(this.getClass().getClassLoader().getResourceAsStream("datasource.properties"));
            String driver = pros.getProperty("driver");
            String url = pros.getProperty("url");
            String username = pros.getProperty("username");
            String password = pros.getProperty("password");
            logger.info(driver);
            logger.info(url);
            logger.info(username);
            logger.info(password);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

执行结果 :
在这里插入图片描述
存数据

    @Test
    public void testProperties1() {
        Properties pros = new Properties();
        pros.setProperty("driver", "com.mysql.jdbc.Driver");
        pros.setProperty("url", "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF8");
        pros.setProperty("username", "root");
        pros.setProperty("password", "7777");
        FileOutputStream os = null;
        try {
            os = new FileOutputStream("D:\\code\\WNXA30-Spring\\spring-02-di\\src\\main\\resources\\datasource.properties");
            pros.store(os,"datasource configuration");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

执行结果:
在这里插入图片描述
stringProperties:(public Set<String> stringPropertyNames())

public class PropertiesTest {
	public static void main(String[] args) {
		Properties p = new Properties();

		p.setProperty("tom", "11");
		p.setProperty("jack", "12");
		p.setProperty("frank", "13");

		//stringPropertyNames相当于KeySet()
		Set<String> set = p.stringPropertyNames();
		for (String key : set) {
			String value = p.getProperty(key);
			System.out.println(key + ":" + value);
		}
	}
}

执行结果:
在这里插入图片描述
store:将Properties中的键值对储存到配置文件,在idea中,保存信息到配置文件,如果含有中文,会储存为Unicode码(public void store( OutputStream out,string comments)):

public static void main(String[] args) throws Exception {
	Properties p = new Properties();

	p.setProperty("tom", "11");
	p.setProperty("jack", "12");
	p.setProperty("frank", "13");

	p.store(new FileOutputStream("user.properties"),"acoffee");
}

执行结果:
在这里插入图片描述
load:加载配置文件的键值对到Properties对象(public void load(InputStream inStream)):

	public static void main(String[] args) throws Exception {
		Properties p = new Properties();

		p.setProperty("tom", "11");
		p.setProperty("jack", "12");
		p.setProperty("frank", "13");
	
		p.load(new FileInputStream("user.properties"));
		System.out.println(p);
	}

执行结果:
在这里插入图片描述
list:将数据显示到指定设备

public static void main(String[] args) throws Exception {
	Properties p = new Properties();
	p.load(ClassStreamTest.class.getResourceAsStream("user.properties"));
	
	p.list(System.out);
}

执行结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值