package com.tms.apitest.common; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ClassLoaderTest { SetLog4j log = new SetLog4j(); public String getProperties(String filename, String param) throws IOException { // 通过Class 获取资源 String propertiesValue = null; // java.net.URL url = ClassLoaderTest.class.getResource("ClassLoaderTest.class"); //log.loggerInfo(url.toString()); // 通过user.dir获取路径 String path = System.getProperty("user.dir"); Properties prop = new Properties(); // 通过File方式获取conf.properties String confPath = path.concat(File.separator).concat("src") .concat(File.separator) .concat("test").concat(File.separator).concat("resources") .concat(File.separator).concat(filename); //log.loggerInfo(confPath); File file = new File(confPath); FileInputStream is = null; if (file.exists() && !file.isDirectory()) { try { is = new FileInputStream(file); prop.load(is); //log.loggerInfo("File Load Successful. " + param + " is: [" // + prop.getProperty(param) + "]"); propertiesValue = prop.getProperty(param); prop.clear(); } catch (FileNotFoundException e) { e.printStackTrace(); log.loggerInfo("File not Found Exception."); } catch (Exception e) { e.printStackTrace(); log.loggerInfo("Properties load Exception."); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } else { log.loggerInfo("Can not find properties from [" + confPath + "]"); } return propertiesValue; } }