java load propertites file

本文介绍了一个用于加载配置文件的工具类PropertiesUtil。该工具能够自动加载位于/WEB-INF/classes目录下的默认配置文件commons-config.properties,并提供了加载指定配置文件的功能。

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

 

在平常的项目中,我们经常需要用到一些配置文件,而加载配置文件的方法都差不多,所以就写了一个

PropertiesUtils来加载配置文件

 

package com.chen.aq.utils;

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

public class PropertiesUtil {
	private static Properties properties = null;
	private static final String file = "/commons-config.properties";
	static{
		loadProperties();
	}
	/**
	 * 读取默认的配置文件信息,默认的配置文件为/WEB-INF/classes/commons-config.properties
	 * @return 
	 */
	public static void loadProperties(){
		StringBuilder sb = getBasePath().append(file);
		InputStream input = null;
		try {
			input = new FileInputStream(sb.toString());
			properties = new Properties();
			properties.load(input);
		} catch (FileNotFoundException e) {
			System.err.println("找不到配置文件:" + file);
		} catch (IOException e) {
			System.err.println("读取配置文件时出错:" + file);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				System.err.println("关闭配置文件:" + file + " 时出错!");
			}
		}
	}
	
	private static StringBuilder getBasePath(){
//		StringBuilder sb = new StringBuilder(PropertiesUtil.class.getResource("").getPath().replace("%20", " "));
//		if(sb.indexOf("classes")!=-1){
//			sb.delete(sb.indexOf("classes"),sb.length());
//		}
//		
//		sb.append("classes");
		//class.getClassLoader().getResource() :names are relative to classpath,but
		//class.getResource() : names are relative to the class's package
		StringBuilder sb = new StringBuilder(PropertiesUtil.class.getClassLoader().getResource("").getPath().replace("%20", " "));
		return sb;
	}
	
	/**
	 * 读取配置文件信息
	 * 
	 * @param fileName 配置文件路径名称,配置文件路径为/WEB-INF/classes
	 * @return Properties
	 */
	public static Properties loadProperties(String fileName){
		StringBuilder sb = getBasePath();
		if(fileName!=null && fileName.startsWith("/")){
			sb.append(fileName);
		}else{
			sb.append("/").append(fileName);
		}
		InputStream input = null;
		try {
			input = new FileInputStream(sb.toString());
			Properties properties = new Properties();
			properties.load(input);
			return properties;
		} catch (FileNotFoundException e) {
			System.err.println("找不到配置文件:" + fileName);
		} catch (IOException e) {
			System.err.println("读取配置文件时出错:" + fileName);
		} finally {
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception e) {
				System.err.println("关闭配置文件:" + fileName + " 时出错!");
			}
		}
		return null;
	}
	
	public static void setPropertyToFile(String key, String value){
		if(properties==null){
			return;
		}
		properties.setProperty(key, value);
		FileOutputStream out = null;
		try {
			URL resource = PropertiesUtil.class.getClassLoader().getResource(file);
			out = new FileOutputStream(resource.getPath());
			properties.store(out, null);
		} catch (FileNotFoundException e) {
			System.err.println("找不到配置文件:" + file);
		} catch (IOException e) {
			System.err.println("设置配置文件时出错:" + file);
		} finally{
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					System.err.println("关闭配置文件:" + file + " 时出错!");
				}
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值