【问题标题】:Unable to read value from properties file in java无法从java中的属性文件中读取值
【发布时间】:2013-10-25 21:05:57
【问题描述】:

实际上,我正在尝试从 JSP 页面获取值并将这些数据写入 config.properties 文件。 我可以在其中写入数据。 但我无法从该 config.properties 文件中读取属性值。

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

但我无法读取“Suite”属性值..

【问题讨论】:

    标签: java jakarta-ee selenium automation properties-file


    【解决方案1】:

    您需要将您的 configPath 更改为:

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

    然后像这样加载它:

    CONFIG.load(new FileInputStream(configPath));
    

    编辑:根据@sircapsalot,绝对路径是个坏主意。如果您认为您的 configPath 会改变,您可以使用它使用正则表达式或类似的东西将其更改为绝对路径,然后将其传递给FileInputStream。我应该明确一点,FileInputStream 确实需要绝对路径才能工作。

    【讨论】:

    • 这很糟糕.. 不推荐这样的绝对路径!使用ClassLoaders
    【解决方案2】:

    试试

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

    【讨论】:

      猜你喜欢
      • 2018-12-23
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      相关资源
      最近更新 更多