1:GetPropertiesValue.java
import java.util.Properties;
public class GetPropertiesValue {
/**
*
* @Title: getValue
* @Description: 根据配置文件路径,文件中的KEY获取对应的value
* @author sizk
* @param fileNmae 配置文件名称
* @param key 需要的key
* @return
* @return String 返回对应的value
* @see
* @throws
* @since 创建 2017年2月8日 11:17:41
* 例如:
* fileName ftp.properties
* key cintractDownImg
*/
public static String getValue(String fileNmae,String key) {
Properties prop = UtilsProperties.getPropertyFile(GetPropertiesValue.class, fileNmae);
return prop.getProperty(key);
}
}
2:utilsProperties.java
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class UtilsProperties {
/**
* 根据clazz的物理路径,获取propertyFileName名称的属性文件的Properties对象
* @param clazz
* @param propertyFileName
* @return
*/
public static Properties getPropertyFile(Class clazz,String propertyFileName){
InputStreamReader reader= new InputStreamReader(clazz.getClassLoader().getResourceAsStream(propertyFileName));
// InputStream inputStream = clazz.getClassLoader().getResourceAsStream(propertyFileName);
Properties p = new Properties();
try {
// p.load(inputStream);
p.load(reader);
} catch (IOException e) {
e.printStackTrace();
}
return p;
}
public static void main(String[] args) {
System.out.println(""+File.separator);
}
}
用法: