在Java中,如何读取与写入*.properties资源文件

在某些Java程序或者框架中,它们在启动时都需要读配置文件,而一般这种配置文件为*.properties文件。

在Java中可以使用Properties类来读取这个文件,Properties这个类继承自HashTable,这种属性文件又以key-value方式存储内容。

既然Properties作文一个文件,那么它就可以通过FileInputstream来读取,

package com.apesource;

public class Demo04 {
	public static void main(String[] args) {
        try (BufferedInputStream bis = new BufferedInputStream(new                     
                   FileInputStream("d:\\IOTest\\demo.propertis"))) {
			int data;
			while((data = bis.read()) != -1) {
				System.out.println((char)data);
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

这种读取方式拿到的只是单个字节信息,并没有按照所存储的key-value方式读取,所以,我们应该通过Properties类来读取,正是因为Properties类继承自HashTable,所以,相应的Properties类中同样有put()和get()方法,Properties类提供了load()方法,通过Inputstream流作为数据源,再向数据源中传入一个输入流,把输入流中的内容传给内部的key和value中,通过这样便可以读取到properties文件中的内容了。

具体实例代码如下:

package com.apesource;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Properties格式文件的读取
 * @author wyh
 *
 */
public class Demo03 {
	public static void main(String[] args) {
		
		// Properties格式文件的读取
		// 创建输入流
		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d:\\IOTest\\data.properties"))) {
			// 创建Properties类型的对象
			Properties props = new Properties();
			
			// 将“输入流”加载至properties集合对象中
			props.load(bis);
			
			// 根据key,获取value
			System.out.println(props.get("tuji"));
			System.out.println(props.get("chunlv"));
			
			// 直接打印集合
			System.out.println(props);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}

关于properties文件的写入,Properties类中也提供了store()方法,使用OutputStream和String类型的注释作为数据源,将内部的键值对集合和注释传入到输出流中,通过这样就可以写入操作了。

具体代码如下:

package com.apesource;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Demo03 {
	public static void main(String[] args) {
		
    // Properties格式文件的写入
		
		try {
			Properties props = new Properties();
			props.put("F1", "1024");
			props.put("F2", "2048");
			props.put("F3", "4096");
			props.put("F4", "10024");
			props.put("F5", "20048");
			
			// 使用“输出流”,将Properties集合中的KV键值对,写入*.Properties文件中
			props.store(new BufferedOutputStream(new FileOutputStream("d:\\IOTest\\demo.properties")), "Apeource");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

执行之后可以看到,我们已经在指定位置写入了一个properties文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值