Properties的使用在java中

希望一下内容能对你对学习和使用properties文件能有所帮助

一、在java中properties类的简单应用

1.Properties类的介绍:properties 类是表示一个持久的属性集,Properties可以保存在流中或者是从流中加载,属性列表中每一个键及对应的值都是一个字符串
2.properties的特点:
①properties是Hashtable的子类,map集合中的方法都可以使用,
②该集合没有泛型,键值都是字符串
③它是一个可以持久化的属性经,键值可以存储到集合中,也可以存储带持久化的设备中(硬盘,U盘,光盘上),键值的来源也是可以是持久化的设备。
④有和流技术相结合的方法
3.properties文件作为键值对储存的配置文件,使用起来极其方便
4;读取properties的步骤
①首先要将文件加载在你要加载的类中‘
②创建properties对象
③通过对象来进行将文件传入进来‘
④通过getproprttity方法就可以将properties文件中的内容读取出来
5.下面是进行代码的部分

这是properties文件

#用户名
user.username=zhangsan;
user.userpassworld = 123123
user.sex = 男

这是对properties文件进行解析

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

public class Test {
	static Properties properties = null;
	static {
		try {
			FileInputStream fis = new FileInputStream("test/test.properties");
			 properties = new Properties();
			properties.load(fis);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
	
		//初始化Properties文件
		String property = properties.getProperty("user.username");
		System.out.println(property);
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_44614066/article/details/88573556