2011-10-31 31 views
0
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但不知何故它沒有加載,它將捕獲異常塊。任何建議爲什麼會發生。

回答

1

如果您嘗試將某些資源加載到資源中,它必須位於類路徑上 - 這就是資源定義(給定或取出)。

要從文件系統路徑加載它,請使用load(InputStream)(或load(Reader))。

+0

你可以舉一些例子根據我的代碼。我很困惑如何做到這一點.. – ferhan

+0

得到它的感謝.. – ferhan

+0

你想這樣說...'prop.load(new FileInputStream(CONFIG_DIR +「/」+ CRAWLER4J_PROP));' – ferhan

0

那麼,你會得到什麼樣的例外?文件未找到?這是Web應用程序還是常規Java應用程序的一部分?

確保您有讀取目錄中屬性文件的權限。

編輯: 如果找不到資源,getResourceAsStream將返回null,如其他人所建議的: 1.確保屬性文件位於類路徑中。 2.躲過/ -

 prop.load(Configurations.class.getClassLoader() 
       .getResourceAsStream(CONFIG_DIR + "//" + CRAWLER4J_PROP)); 
+0

我得到java.lang.NullPointerException – ferhan

+0

仍然與您的修改代碼相同的錯誤... – ferhan

+0

OP是專門試圖加載文件系統文件,AFAICT。 –

0

這是最有可能是錯誤配置的路徑,你抓嘗試附加在你的System.err的聲明:

e.getMessage(); 
+0

我得到java.lang.NullPointerException – ferhan

+0

看到這個SO問題:http://stackoverflow.com/questions/4821643/loading-a-configuration-file-from-the-classpath – jpredham

0

我想你可能會發現你需要躲避「/」所以請嘗試使用「//」。我不確定這是否是這個問題,但我已經回到了一段時間,並且試圖弄清楚爲什麼它在某些地方而不是其他地方工作。

相關問題