import static com.crawler.constants.CrawlerConstants;
import static com.crawler.constants.CrawlerConstants.CRAWLER4J;
import java.util.Properties;
public final class Configurations {
private static Properties prop = new Properties();
public static String getStringProperty(String key, String defaultValue) {
if (prop == null || prop.getProperty(key) == null) {
return defaultValue;
}
return prop.getProperty(key);
}
public static int getIntProperty(String key, int defaultValue) {
if (prop == null || prop.getProperty(key) == null) {
return defaultValue;
}
return Integer.parseInt(prop.getProperty(key));
}
public static short getShortProperty(String key, short defaultValue) {
if (prop == null || prop.getProperty(key) == null) {
return defaultValue;
}
return Short.parseShort(prop.getProperty(key));
}
public static long getLongProperty(String key, long defaultValue) {
if (prop == null || prop.getProperty(key) == null) {
return defaultValue;
}
return Long.parseLong(prop.getProperty(key));
}
public static boolean getBooleanProperty(String key, boolean defaultValue) {
if (prop == null || prop.getProperty(key) == null) {
return defaultValue;
}
return prop.getProperty(key).toLowerCase().trim().equals("true");
}
static {
try {
prop.load(Configurations.class.getClassLoader()
.getResourceAsStream(CONFIG_DIR + "/" + CRAWLER4J));
} catch (Exception e) {
prop = null;
System.err.println("WARNING: Could not find crawler4j.properties file in class path. I will use the default values.");
}
}
}
在我上面的try循環中,它沒有加載crawler4j.properties文件。如前所述,它是這樣的 -無法加載保存在代碼外的屬性文件
try {
prop.load(Configurations.class.getClassLoader()
.getResourceAsStream("crawler4j.properties"));
}
所以它能夠直接從src/main/resources文件夾加載它。但是我想將這個crawler4j.properties文件保存在不同目錄的代碼之外。所以我一直保存crawler4j.properties文件中/my/dir/crawler4j.properties,這是嘗試循環,我想更改 -
try {
prop.load(Configurations.class.getClassLoader()
.getResourceAsStream(CONFIG_DIR + "/" + CRAWLER4J_PROP));
}
而且CONFIG_DIR包含\我的\目錄和CRAWLER4J具有crawler4j.properties但不知何故它沒有加載,它將捕獲異常塊。任何建議爲什麼會發生。
你可以舉一些例子根據我的代碼。我很困惑如何做到這一點.. – ferhan
得到它的感謝.. – ferhan
你想這樣說...'prop.load(new FileInputStream(CONFIG_DIR +「/」+ CRAWLER4J_PROP));' – ferhan