Java中Properties 类的详细使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java中Properties 类的详细使用

两个蝴蝶飞   2021-01-14 我要评论
这篇文章主要介绍了Java中Properties 类的详细使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

我把你的头像,设置成我的名字,此刻你便与我同在。
我把你的名字,写进我的代码里面,以后,我的世界便存在着你。

一.Properties 类

Properties 类位于 java.util.Properties ,是Java 语言的配置文件所使用的类, Xxx.properties 为Java 语言常见的配置文件,如数据库的配置 jdbc.properties, 系统参数配置 system.properties。 这里,讲解一下Properties 类的具体使用。
以key=value 的 键值对的形式进行存储值。 key值不能重复。

在这里插入图片描述

继承了Hashtable 类,以Map 的形式进行放置值, put(key,value) get(key)

主要方法:

1. 构造方法

这里只讲解一些常用的形式。

二. 打印 JVM 参数

JVM 中可以获取Properties, 来打印输出 JVM 所了解的属性值。
用list() 方法,打印到控制台。

@Test
	public void printTest(){
		Properties properties=System.getProperties();
		properties.list(System.out);
	}

常见的有:

在这里插入图片描述

三.打印自定义.properties 文件中的值

在src 目录下,放置 jdbc.properties 文件,是数据库的配置文件。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8
jdbc.username=root
jdbc.password=abc123

三.一 list 输出到控制台 用绝对路径加载

@Test
	public void name1Test(){
		try{
			Properties properties=new Properties();
			//用的是磁盘符的绝对路径 
			InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties"));
			properties.load(input);
			properties.list(System.out);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

url 被截取了。

在这里插入图片描述

三.二 propertyNames 输出 getClass() 加载

@Test
	public void name2Test(){
		try{
			Properties properties=new Properties(); // 用/文件名, / 表示根目录
			InputStream input=PropertiesTest.class.getClass().getResourceAsStream("/jdbc.properties");
			properties.load(input);
			Enumeration<String> names=(Enumeration<String>) properties.propertyNames();
			while(names.hasMoreElements()){
				//这是key值
				String key=names.nextElement();
				String value=properties.getProperty(key);
				System.out.println(key+"="+value);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

在这里插入图片描述

三.三 stringPropertyNames 输出 getClassLoader 加载 (推荐)

@Test
	public void name3Test(){
		try{
			Properties properties=new Properties();
			//直接写src 类路径下的文件名
			InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
			properties.load(input);
			
			//把key值转换成set 的形式,遍历set
			Set<String> names=properties.stringPropertyNames();
			Iterator<String> iterator=names.iterator();
			while(iterator.hasNext()){
				String key=iterator.next();
				String value=properties.getProperty(key);
				System.out.println(key+"="+value);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

在这里插入图片描述

四. 获取值 getProperties

@Test
	public void name3Test(){
		try{
			Properties properties=new Properties();
			InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
			properties.load(input);
			//String value=properties.getProperty("jdbc.url");
			String value=properties.getProperty("jdbc.url1","没有该key值");
			System.out.println("输出值:"+value);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

输出时,getProperty() 有当前的key值,则输出Key值对应的value 值。
如果没有key值,则输出 null 值。
后面可以跟 default 值,如果没有该值,则输出设置的默认值。

在这里插入图片描述

五. 写入到Properties 文件

五.一 普通写入,中文时乱码

@Test
	public void writeTest(){
		try{
			Properties properties=new Properties();
			InputStream input=PropertiesTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
			properties.load(input);
			
			//多添加几个值。
			properties.setProperty("name","两个蝴蝶飞");
			properties.setProperty("sex","男");

			//properties.put("name","两个蝴蝶飞"); 可以用继承Hashtable 的put 方法写入值
			// properties.put("sex","男");
			
			//将添加的值,连同以前的值一起写入 新的属性文件里面。
			OutputStream out=new FileOutputStream("D:\\jdbc.properties");
			properties.store(out,"填充数据");
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

在这里插入图片描述

五.二 解决乱码写入的问题

在构建输入流和输出流时,指定编码格式, 编码的格式相同。 如均是 utf-8的形式。

@Test
	public void write2Test(){
		try{
			Properties properties=new Properties();
			//用绝对路径
			InputStream input=new BufferedInputStream(new FileInputStream("D:\\workspace\\JavaLearn\\src\\jdbc.properties"));
			properties.load(new InputStreamReader(input,"utf-8"));
				//多添加几个值。
			properties.setProperty("name","两个蝴蝶飞");
			properties.setProperty("sex","男");
			
			OutputStream output=new FileOutputStream("D:\\jdbc.properties");
			OutputStreamWriter out=new OutputStreamWriter(output,"utf-8");
			properties.store(out,"填充数据");
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

测试运行之后:

在这里插入图片描述

这样便解决了乱码的问题。

六 . 加载和导出到 xml 配置文件

六.一 导出到 .xml 配置文件 storeToXML

将Properties 类中定义的属性,导出成 .xml 的形式.

@Test
	public void xmlWriteTest(){
		try{
			//处理成编码样式。
			Properties properties=new Properties();
			
				//多添加几个值。
			properties.setProperty("name","两个蝴蝶飞");
			properties.setProperty("sex","男");
			OutputStream output=new FileOutputStream("D:\\jdbc.xml");
			//编码设置成utf-8的形式。 
			properties.storeToXML(output,"填充到xml","utf-8");
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

测试结果为:

在这里插入图片描述

用 <entry> 节点 key为属性, 后面跟值来进行输入。
可按照这种形式,继续添加。

六.二 导出XML 配置文件 loadFromXML

@Test
	public void xmlReadTest(){
		try{
			Properties properties=new Properties();
			InputStream input=new BufferedInputStream(new FileInputStream("D:\\jdbc.xml"));
			properties.loadFromXML(input);
			properties.list(System.out);
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}

在这里插入图片描述

这就是Properties 类的常见用法 。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们