获取properties文件值得工具类
import java.io.File;
import java.util.Properties;
public class PropertiesUtil {
public String getServiceConfigValue(String id,String key){
//获取平台下的文件分隔符。
char separa = File.separatorChar;
//Object configKeyObj = map.get(sKey);
//String key = String.valueOf(configKeyObj);
String url =this.getClass().getClassLoader().getResource("/").getPath();
url = url.substring(0,url.indexOf("WEB-INF"));
url += "WEB-INF" + separa + "serviceconfig" + separa+ "CHS" + separa + id + ".properties";
File file = new File(url);
Properties proFile = FileUtil.getProperties(file);
String objValue = String.valueOf(proFile.get(key));
return objValue;
}
}
Java获取代码
PropertiesUtil prosUtil = new PropertiesUtil();
msg = prosUtil.getServiceConfigValue("regist", code);
fileUtil.getProperties
public static InputStream getFileAsStream(File file)
{
Assert.notNull(file);
try {
return file.toURL().openStream();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Properties getProperties(File file)
{
Assert.notNull(file);
Properties properties = new Properties();
InputStream inputStream = getFileAsStream(file);
try {
properties.load(inputStream);
inputStream.close();
return properties;
} catch (IOException e) {
throw new RuntimeException(e);
}
}