java项目或者javaweb项目,读取配置文件,的参数配置

java项目,读取配置文件,的参数配置

package com.hndist.tsps.tool;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Properties;

import org.wltea.analyzer.core.IKSegmenter;

public class PropertiesUtil {
	
	private static Properties systemProperties = new Properties();
	
	/**
	 * 初始化配置文件
	 */
	public static void init() 
{
	    try 
		{
	    	//读取系统配置文件
	    	InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("config/config.properties");
	    	systemProperties.load(in);
	    	//System.out.println("已加载系统配置文件...");
	    	//读取索引配置文件
	    	String indexPath = getValue("SEARCH_INDEX_PATH");
	    	//检测分词词典
			if(existFile(indexPath,".dic"))
			{
				System.out.println("检测到有用户自定义词典...");
				String epath = indexPath+"//"+"ext_user.dic";
				epath = epath.replaceAll("//", "/");
				
				//加载分词词典
				IKSegmenter ikSeg = new IKSegmenter(new StringReader("河南数慧信息技术有限公司"),true,epath);
				System.out.println(ikSeg.toString());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			systemProperties = null;
			System.out.println("读取配置文件信息失败!!!");
		}
	}
	
	/**
	 * 是否存在对应文件
	 * @param path
	 * @param suffix
	 * @return boolean
	 */
	public static boolean existFile(String path,String suffix)
	{
		File file = new File(path);
		if(file.isDirectory())
		{
			File[] files = file.listFiles();
			for(File f:files)
			{
				if(f.getAbsolutePath().endsWith(suffix))
				{
					return true;
				}
			}
		}
		else
		{
			if(file.getAbsolutePath().endsWith(suffix))
			{
				return true;
			}
		}
	    return false;
	}	
	/**
	 * 获取对应KEY的值
	 * @param key
	 * @return String
	 */
	public static String getValue(String key)
	{
		if(systemProperties != null) 
		{
			String value = systemProperties.getProperty(key, null);
			return value;
		}
		else
		{
			return null;
		}
	}
	
}

配置文件格式
参数名=值
调用getValue方法,传入参数名,返回值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值