java使用java.util.Properties读取properties文件的九种方法

直接上代码:

package com.test.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.springframework.core.io.support.PropertiesLoaderUtils;

public class TestProperties {
	private static TestProperties testProperties = new TestProperties();
	public static void main(String[] args) {
		//获取properties配置文件中的值
		Properties prop = new Properties();
		try {
			prop.load(test1());//包含2种方法
			prop.load(test2());//包含2种方法
			prop.load(testProperties.test3());//包含2种方法
			//使用spring-core包封装好的方法
			prop = PropertiesLoaderUtils.loadAllProperties("test.properties");
			Enumeration<?> e = prop.propertyNames();
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				System.out.println(key+"="+new String(prop.getProperty(key).getBytes("ISO-8859-1"),"UTF-8"));
			}
			test4();
			test5();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 使用FileInputStream文件输入流
	 * @return
	 */
	public static InputStream test1(){
		InputStream in = null;
		try {
			//此处是相对于项目的相对路径
			//in = new FileInputStream("src/main/resources/test.properties");
			//或
			in = new BufferedInputStream(new FileInputStream("src/main/resources/test.properties"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
        return in;
	}
	/**
	 * 使用ClassLoader
	 * 默认从classPath路径下找文件
	 * @return
	 */
	public static InputStream test2(){
		//InputStream in = ClassLoader.getSystemResourceAsStream ("test.properties");
		//或
		InputStream in = testProperties.getClass().getClassLoader().getResourceAsStream("test.properties");
		return in;
	}
	/**
	 * 使用class变量的getResourceAsStream()方法
	 * 文件名前不加“/”,则表示从当前类所在的包下查找该资源
	 * 文件名前加了“/”,则表示从classPath路径下查找资源
	 * @return
	 */
	public InputStream test3(){
		//InputStream in = getClass().getResourceAsStream("/test.properties");
		//或
		InputStream in = TestProperties.class.getResourceAsStream("/test.properties");
		return in;
	}
	/**
	 * 使用java.util.ResourceBundle类的getBundle()方法
	 * Locale.getDefault():没有提供语言和地区的资源文件是系统默认的资源文件
	 * test:不需要文件的后缀
	 */
	public static void test4(){
		try {
			ResourceBundle rb = ResourceBundle.getBundle("test", Locale.getDefault());
			Enumeration<String> e1 = rb.getKeys();
			while (e1.hasMoreElements()) {
				String key = e1.nextElement();
				System.out.println(key+"="+new String(rb.getString(key).getBytes("ISO-8859-1"),"UTF-8"));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 使用java.util.PropertyResourceBundle类的构造函数
	 */
	public static void test5(){
		InputStream in = ClassLoader.getSystemResourceAsStream ("test.properties");
		try {
			ResourceBundle rb = new PropertyResourceBundle(in);
			Enumeration<String> e1 = rb.getKeys();
			while (e1.hasMoreElements()) {
				String key = e1.nextElement();
				System.out.println(key+"="+new String(rb.getString(key).getBytes("ISO-8859-1"),"UTF-8"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}

test.properties文件中的内容是:
name=天若有情
password=天亦老
运行程序后控制台输出 test.properties文件中的内容。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值