package config; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ConfigManager { private static final Properties properties = new Properties(); static { try (InputStream input = ConfigManager.class.getClassLoader().getResourceAsStream("config/config.properties")) { if (input == null) { System.err.println("无法找到配置文件 config.properties"); } else { properties.load(input); } } catch (IOException e) { System.err.println("读取配置文件失败: " + e.getMessage()); } } /** * 获取配置值 * @param key 配置键 * @param defaultValue 默认值 * @return 配置值 */ public static String getProperty(String key, String defaultValue) { return properties.getProperty(key, defaultValue); } /** * 获取整数配置值 * @param key 配置键 * @param defaultValue 默认值 * @return 配置值 */ public static int getIntProperty(String key, int defaultValue) { String value = properties.getProperty(key); if (value != null) { try { return Integer.parseInt(value); } catch (NumberFormatException e) { System.err.println("配置值格式错误: " + key + " = " + value); } } return defaultValue; } /** * 获取基础URL * @return 基础URL */ public static String getBaseUrl() { return getProperty("base.url", "https://fanqienovel.com"); } /** * 获取人气榜页面URL * @return 人气榜页面URL */ public static String getRankingUrl() { return getProperty("ranking.url", "/rank/hot"); } /** * 获取排行榜API URL * @return 排行榜API URL */ public static String getRankingApiUrl() { return getProperty("ranking.api.url", "/api/rank/hot"); } /** * 获取爬取间隔 * @return 爬取间隔(毫秒) */ public static int getCrawlInterval() { return getIntProperty("crawl.interval", 2000); } /** * 获取最大爬取小说数量 * @return 最大爬取小说数量 */ public static int getMaxNovelCount() { return getIntProperty("max.novel.count", 20); } /** * 获取输出目录 * @return 输出目录 */ public static String getOutputDir() { return getProperty("output.dir", "output"); } }