2013-10-17 97 views
0

其實我試圖從JSP頁面獲取值並將這些數據寫入config.properties文件。 我可以在其中寫入數據。 但我無法從該config.properties文件讀取屬性值。無法從java中的屬性文件讀取值

public static void startup() throws ReflectiveOperationException, Exception { 
     String configPath ="C:/Selenium_Automation_JSP/automation/src/com/selenium/config/config.properties"; 
     DriverScript Test = new DriverScript(configPath); 
     System.out.println("Config path loaded"); 
     Test.start(configPath); 
    } 

     public void start(String configPath) throws ReflectiveOperationException, IllegalArgumentException, Exception{ 



      System.out.println(configPath); 

      CONFIG=new Properties(); 
      FileInputStream fs = new FileInputStream(configPath); 
      CONFIG.load(fs); 

      String mapFile=CONFIG.getProperty("Suite"); 

      System.out.println(mapFile); 
      SuiteXLS = new Xls_Reader(mapFile); 

在控制檯中,我可以看到config.properties的路徑。但是在讀取文件時,它顯示了以下錯誤,

Config path loaded 
C:/Selenium_Automation_JSP/automation/src/com/selenium/config/config.properties 
null 
java.lang.NullPointerException 
    at java.io.FileInputStream.<init>(FileInputStream.java:124) 
    at java.io.FileInputStream.<init>(FileInputStream.java:87) 
    at com.selenium8x8.xlsoperations.Xls_Reader.<init>(Xls_Reader.java:43) 
    at com.selenium8x8.driver.DriverScript.start(DriverScript.java:106) 
    at com.selenium8x8.driver.DriverScript.startup(DriverScript.java:85) 
    at com.selenium8x8.servlet.ControlServlet.doPost(ControlServlet.java:120) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:724) 

這是我config.properties文件

TestURL=http://agents.8x8pilot.com/ 
Suite=C:/Selenium_Automation_JSP/automation/src/com/selenium/xls/Suite.xlsx 
ObjectRepository=C:/Selenium_Automation_JSP/automation/src/com/selenium/xls/ObjectRepository.xls 
DataManagement=C:Selenium_Automation_JSP/automation/src/com/selenium/xls/StoredData.xls 

但我不能能夠讀取「套房」的屬性值。 。

回答

-1

您需要你用configPath更改爲:

String configPath ="C:\\Selenium_Automation_JSP\\automation\\src\\com\\selenium\\config\\config.properties"; 

,然後加載它想:

CONFIG.load(new FileInputStream(configPath)); 

編輯:按@sircapsalot,絕對路徑是一個壞主意。如果你認爲你的configPath會改變,你可以使用它將其更改爲絕對路徑使用正則表達式或類似的東西,然後將其傳遞給FileInputStream。我應該清楚地知道FileInputStream確實需要絕對路徑才能工作。

+0

而我依然面臨着同樣的問題.. – Prasanna

+0

這是不好的..不建議這樣的絕對路徑!使用'ClassLoader's – sircapsalot

0

嘗試

CONFIG=new Properties(); 
CONFIG.store(new FileOutputStream(configPath), null); 
CONFIG.getProperty("Suite");