测试路径:
public static void main(String[] args) {
String path1 = System.getProperty("user.home");//当前登录用户目录
String path2 = System.getProperty("user.dir");//jar包所在目录名
System.out.println(path1+"=="+path2);
}
使用列子:
public static void main(String[] args) {
try {
PropContainer pro = PropContainer.getInstance(System
.getProperty("user.dir")
+ File.separator
+ "config.properties");
InitExcelData.init(pro.getProperty("excelPath"));
String sourcePath = pro.getProperty("sourcePath");
String tempPath = pro.getProperty("tempPath");
String destPath = pro.getProperty("destPath");
String errorPath = pro.getProperty("errorPath");
String bakPath = pro.getProperty("bakPath");
String isBak = pro.getProperty("isBak");
} catch (Exception e) {
log.info(e.getMessage());
e.printStackTrace();
}
}
public class PropContainer {
private static final Logger log = Logger.getLogger(PropContainer.class);
private Properties properties;
private static String charset = "UTF-8";
private static Map<String, PropContainer> map = new HashMap<String, PropContainer>();
private PropContainer(String propertiesWay) {
initConfig(propertiesWay);
}
public static PropContainer getInstance(String propertiesWay) {
if (map.containsKey(propertiesWay)) {
return map.get(propertiesWay);
}
PropContainer p = new PropContainer(propertiesWay);
map.put(propertiesWay, p);
return p;
}
private void initConfig(String propertiesWay) {
properties = new Properties();
try (InputStreamReader in = new InputStreamReader(new FileInputStream(
propertiesWay), charset)) {
properties.load(in);
} catch (Exception e) {
log.error("", e);
}
}
public String getProperty(String key, String defaultValue) {
if (properties == null) {
return defaultValue;
}
return properties.getProperty(key, defaultValue);
}
public String getProperty(String key) throws RuntimeException{
String tmp = properties.getProperty(key);
if(StringUtils.isBlank(tmp)){
throw new RuntimeException("属性为空!"+key);
}
return tmp;
}
public int getInt(String key, int defaultValue) {
String prop = getProperty(key, "");
if ("".equals(prop) || !NumberUtils.isDigits(prop)) {
return defaultValue;
}
return Integer.parseInt(prop);
}
public boolean containsKey(String key) {
if (properties != null && properties.containsKey(key)) {
return true;
}
return false;
}
}