Properties获取配置文件的值(路径简单,建议使用)

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

import org.apache.ibatis.io.Resources;


public class MybatisPropertiesUtil {

	/**
	 * 根据Key读取全局配置文件的value
	 * 
	 * @param key
	 * @return
	 */
	public static String getValueByKey(String file ,String key) {
		Properties props = new Properties();
		InputStream in = null;
		try {
			in = Resources.getResourceAsStream(file);
			props.load(in);
			return props.getProperty(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "";
	}


测试: 两个地方配置文件获取的方式




猜你喜欢

转载自blog.csdn.net/weixin_42245930/article/details/81035284