properties文件读取工具类

来源:互联网 发布:小红书是什么软件 编辑:程序博客网 时间:2024/06/05 10:02

PropertiesLoader

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URISyntaxException;import java.util.Properties;import java.util.logging.Logger;/** * properties文件读取工具类 *  * @author -- *  */public class PropertiesLoader {private static Logger logger = Logger.getLogger("PropertiesLoader");private static Properties configProps = PropertiesLoader.loadProperties("config.properties");/** 日志存放路径 */public static String getLogPath() {return configProps.getProperty("logPath");}/** 爬虫系统访问入口 */public static String getCrawlerUrl() {return configProps.getProperty("crawlerUrl");}public static Properties loadProperties(String filePath) {Properties props = null;if (filePath == null || filePath.equals("")) {logger.warning("that path of properties file is null");return props;}URI rootPath = null;try {rootPath = PropertiesLoader.class.getClassLoader().getResource("").toURI();} catch (URISyntaxException e) {e.printStackTrace();}String path = rootPath.getPath() + filePath;File file = new File(path);if (!file.exists()) {logger.warning("that properties file is not found");return props;}props = new Properties();InputStream is = null;try {is = new FileInputStream(file);props.load(is);} catch (FileNotFoundException e) {logger.warning("that properties file is not found");e.printStackTrace();} catch (IOException e) {logger.warning("that properties file was loading error");e.printStackTrace();} finally {try {is.close();} catch (IOException e) {logger.warning("that fileInputStream of properties was closeing error");e.printStackTrace();}}return props;}}


原创粉丝点击