Java Property类使用详解

 更新时间:2019年04月08日 09:12:26   作者:CS-Bob  
这篇文章主要介绍了Java Property类使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

概念理解

Properties 继承于 Hashtable。表示一个持久的属性集,属性列表以key-value的形式存在,key和value都是字符串。Properties类被许多Java类使用。例如,在获取环境遍历时它就作为System.getProperties()方法的返回值。我们在很多需要避免硬编码的应用场景下需要使用Properties文件来加载程序需要配置的信息,比如JDBC、MyBatis框架等。Properties类则是Properties文件和程序的中间桥梁,不论是从properties文件读取信息还是写入信息到properties文件都要经由Properties类。

写入

Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。

我们写一个类测试

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Properties;

public class TestProperties {
	public void writeProperties() {
		Properties properties = new Properties();
		OutputStream output = null;
		try {
			output = new FileOutputStream("config.properties");
			properties.setProperty("url", "jdbc:mysql://localhost:3306/");
			properties.setProperty("username", "root");
			properties.setProperty("password", "root");
			properties.setProperty("databases", "music_player");
			properties.store(output, "Steven1997 modify" + new Date().toString());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(output!=null) {
				try {
					output.close();
				}catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	public static void main(String[] args) {
		TestProperties t = new TestProperties();
		t.writeProperties();
	}
}

执行后,工程下面会出现一个config.properties文件,属性文件内容如下:

读取

使用getProperty获取config.properties文件配置文件的各项属性。

package property;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LoadProperties {
	public void loadProperties() {
		Properties properties = new Properties();
		InputStream inputStream = null;
		
		try {
			inputStream = new FileInputStream("config.properties");
			properties.load(inputStream);
			System.out.println("url:" + properties.getProperty("url"));
			System.out.println("username:" + properties.getProperty("username"));
			System.out.println("password:" + properties.getProperty("password"));
			System.out.println("database:" + properties.getProperty("database"));
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(inputStream !=null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	public static void main(String[] args) {
		LoadProperties l = new LoadProperties();
		l.loadProperties();
	}
}

运行后的结果

url:jdbc:mysql://localhost:3306/
username:root
password:root
database:music_player

遍历

遍历属性文件中的键值对

package property;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class PropertiesTest {
	public void printAll() {
		Properties prop = new Properties();
		InputStream input = null;
		try {
			String file = "config.properties";
			input = getClass().getClassLoader().getResourceAsStream(file);
			if(input == null) {
				System.out.println("无法加载文件" + file);
				return ;
			}
			prop.load(input);
			// 方法一
			Set<Object> keys = prop.keySet();
			for(Object key:keys) {
				System.out.println("key:" + key.toString() + "|" + "value:" + prop.get(key));
			}
			//方法二:
			Set<Entry<Object, Object>> entrys =	prop.entrySet();//返回的属性键值对实体
			for(Entry<Object, Object> entry:entrys){
				System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
			}
			//方法三:
			Enumeration<?> e = prop.propertyNames();
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				String value = prop.getProperty(key);
				System.out.println("Key:" + key + ",Value:" + value);
			}

		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(input != null) {
				try {
					input.close();
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) {
		PropertiesTest p = new PropertiesTest();
		p.printAll();
	}
}

运行结果如下:

key:url|value:jdbc:mysql://localhost:3306/
key:password|value:root
key:database|value:music_player
key:username|value:root
key:url,value:jdbc:mysql://localhost:3306/
key:password,value:root
key:database,value:music_player
key:username,value:root
Key:password,Value:root
Key:url,Value:jdbc:mysql://localhost:3306/
Key:database,Value:music_player
Key:username,Value:root

 properties文件默认的编码格式居然是ISO-8859-1,这样导致往配置文件里面写入中文的时候转换成另一种格式的编码,需要把properties 文件的编码格式改为UTF-8,这样才会让配置文件保存中文数据的时候不会出现转码的问题

以上所述是小编给大家介绍的Java Property类使用详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • jmeter中beanshell的用法小结

    jmeter中beanshell的用法小结

    本文主要介绍了jmeter中beanshell的用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • Java 中引入内部类的意义?

    Java 中引入内部类的意义?

    这篇文章主要介绍了Java 中引入内部类的意义?文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,,需要的朋友可以参考下
    2019-06-06
  • 简单谈谈Struts动态表单(DynamicForm)

    简单谈谈Struts动态表单(DynamicForm)

    下面小编就为大家带来一篇简单谈谈Struts动态表单(DynamicForm)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • java微信公众号支付示例详解

    java微信公众号支付示例详解

    这篇文章主要为大家详细介绍了java微信公众号支付示例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • java8异步调用如何使用才是最好的方式

    java8异步调用如何使用才是最好的方式

    异步调用主要用于当前程序的执行不用等待调用方法执行结束就可以继续执行,下面这篇文章主要给大家介绍了关于java8异步调用如何使用才是最好的方式,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-01-01
  • 深入理解Java中没那么简单的单例模式

    深入理解Java中没那么简单的单例模式

    这篇文章主要给大家详细介绍了Java单例模式,关于Java中的单例模式并非看起来那么简单的,为什么要这么说呢?下面通过这篇文章来一起看看吧,有需要的朋友们可以参考借鉴。
    2017-01-01
  • Java实现FTP文件上传

    Java实现FTP文件上传

    FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”。用于Internet上的控制文件的双向传输。本文将讲解如何用Java实现FTP文件上传
    2021-05-05
  • 关于Maven parent.relativePath说明

    关于Maven parent.relativePath说明

    Maven中的relativePath用于指定父项目pom.xml的相对路径,默认值为../pom.xml,这个配置帮助Maven在构建时定位父模块的位置,确保模块间的依赖关系正确,relativePath可以指向本地或远程仓库中的父项目,如果不需要寻找父项目,可以将其设置为空
    2024-09-09
  • Java Json字符串的双引号(

    Java Json字符串的双引号("")括号如何去掉

    这篇文章主要介绍了Java Json字符串的双引号("")括号如何去掉?具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • Java中StringUtils与CollectionUtils和ObjectUtil概念讲解

    Java中StringUtils与CollectionUtils和ObjectUtil概念讲解

    这篇文章主要介绍了Java中StringUtils与CollectionUtils和ObjectUtil概念,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-12-12

最新评论