properties 文件获取工具类
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * properties文件获取工具类 */ public class PropertiesReader { private static Logger logger = LogManager.getLogger(DelFilesJob.class.getName()); private static Properties props; static { loadProps(); } private static void loadProps() { logger.info("开始加载properties文件内容……"); props = new Properties(); InputStream is = null; try { //通过类加载器进行获取properties文件流 is = PropertiesReader.class.getClassLoader().getResourceAsStream("config.properties"); props.load(is); } catch (FileNotFoundException e) { logger.info("properties文件未找到"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != is) { is.close(); } } catch (IOException e) { logger.info("properties文件流关闭出现异常"); } } logger.info("加载properties文件内容完成……"); } /** * 获取指定的properties文件属性值 * * @param key * @return */ public static String getProperty(String key) { return props.getProperty(key); } /** * 获取指定的properties文件属性值(带默认值) * * @param key * @param defaultValue * @return */ public static String getProperty(String key, String defaultValue) { return props.getProperty(key, defaultValue); } }