import java.io.IOException;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;
public class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties properties= new Properties();
/*properties文件名*/
private static final String PROPERTIES_FILE_NAME="centrin-dao.properties";
public static String getProperty(String name) {
String result = null;
try {
//下载文件
properties = PropertiesLoaderUtils.loadAllProperties(PROPERTIES_FILE_NAME);
result = properties.getProperty(name);//根据name得到对应的value
} catch (IOException e) {
logger.warn(" loadAllProperties error 1");
e.printStackTrace();
}
return result;
}
public static String getProperty(String name ,String properties_name) {
String result = null;
try {
//下载文件
properties = PropertiesLoaderUtils.loadAllProperties(properties_name);
result = properties.getProperty(name);//根据name得到对应的value
} catch (IOException e) {
logger.warn(" loadAllProperties error 1");
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
System.out.println(PropertiesUtil.getProperty("createPlanPath"));
}
}
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;
public class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties properties= new Properties();
/*properties文件名*/
private static final String PROPERTIES_FILE_NAME="centrin-dao.properties";
public static String getProperty(String name) {
String result = null;
try {
//下载文件
properties = PropertiesLoaderUtils.loadAllProperties(PROPERTIES_FILE_NAME);
result = properties.getProperty(name);//根据name得到对应的value
} catch (IOException e) {
logger.warn(" loadAllProperties error 1");
e.printStackTrace();
}
return result;
}
public static String getProperty(String name ,String properties_name) {
String result = null;
try {
//下载文件
properties = PropertiesLoaderUtils.loadAllProperties(properties_name);
result = properties.getProperty(name);//根据name得到对应的value
} catch (IOException e) {
logger.warn(" loadAllProperties error 1");
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws IOException {
System.out.println(PropertiesUtil.getProperty("createPlanPath"));
}
}
Java属性文件加载与使用
本文介绍了如何在Java中加载并获取属性文件的值,包括使用Properties类进行操作及异常处理。
9656

被折叠的 条评论
为什么被折叠?



