Properties类的基础用法和Properties文件

本文介绍Java中Properties类的应用,包括读取和写入配置文件、转换为Map及XML文件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Properties文件

xxx.properties 是java的一种配置文件,内容格式是键值对的方式存在,key = value ;
例如:age = 10 ;
在这里插入图片描述
在开发过程中将一些数据等配置在properties文件中,对项目的后期维护提供了便利。

Properties类的基础用法

Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。Properties类是线程安全的:多个线程可以共享单个Properties对象而无需进行外部同步。

读取Properties文件(根据key值获取value值)

在这里插入图片描述

public class PopUtil {

	public static void main(String[] args) {
		//properties文件放下src目录下
	    //InputStream ins = PopUtil.class.getResourceAsStream("/test.properties");
	    //properties文件放在类的同包下
		InputStream in = PopUtil.class.getResourceAsStream("test.properties");
		Properties p = new Properties();
		try {
			p.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println(p.getProperty("name"));
		System.out.println(p.getProperty("age"));
	}

在这里插入图片描述

将properties文件中的数据存入到Map集合中
public Map<String, String> getMapByProperties(String name) {
		Map<String, String> map = new HashMap<>();
		Properties pop = new Properties();
		InputStream ins =  getClass().getResourceAsStream(name);
		try {
			pop.load(ins);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Set<Entry<Object, Object>> set = pop.entrySet();
		for (Entry<Object, Object> entry : set) {
			map.put((String) entry.getKey(), (String) entry.getValue());
		}
		return map;
	}
将properties文件中的数据转化为XML文件

在项目下新建一个Test.xml的文件
在这里插入图片描述

package com.shang;

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

/**
 * @author shang
 * @date 2019年3月28日
 */
public class PopUtil {
	public static void main(String[] args) throws IOException {
		Properties prop = new Properties();
		InputStream in = PopUtil.class.getResourceAsStream("/test.properties");
		prop.load(in);
		FileOutputStream fos = new FileOutputStream("Test.xml");
		prop.storeToXML(fos, "TEST");
		fos.close();
	}
}

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

将Test.xml中的数据读取出来
public class PopUtil {
	public static void main(String[] args) throws IOException {
		Properties prop = new Properties();
		InputStream in = new FileInputStream("Test.xml");
		prop.loadFromXML(in);
		prop.list(System.out);
		
	}
}

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

给properties文件写入数据

properties文件在项目下
在这里插入图片描述

public class PopUtil {
	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		//加true,追加,不加true,则覆盖
		OutputStream out =new FileOutputStream("test.properties"true);
		properties.setProperty("color", "red");
		properties.store(out, "AAA");
		out.close();
	}		
}

true运行结果:
在这里插入图片描述
不加true的运行结果“:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巴黎有个小铁匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值