简体   繁体   English

无法从Java中的属性文件读取值

[英]Unable to read value from properties file in java

Actually i'm trying to get the values from JSP page and writing those data to config.properties file. 实际上,我正在尝试从JSP页面获取值并将这些数据写入config.properties文件。 I'm able to write the data in it. 我可以在其中写入数据。 But i can't able to read the property values from that config.properties file. 但是我无法从该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);

In console, i can see the path of config.properties. 在控制台中,我可以看到config.properties的路径。 But while reading the file, it shows the following error, 但是在读取文件时,它显示以下错误,

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)

This is my config.properties file 这是我的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

But i can't able to read the "Suite" property value.. 但是我无法读取“ Suite”属性值。

Try 尝试

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

You need to change your configPath to : 您需要将configPath更改为:

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

and then load it like: 然后像这样加载它:

CONFIG.load(new FileInputStream(configPath));

EDIT : As per @sircapsalot , absolute paths is a bad idea. 编辑:根据@sircapsalot,绝对路径是一个坏主意。 If you think your configPath will change, you can use it to change it to an absolute path using regex or something like that and then pass it to the FileInputStream . 如果您认为configPath会更改,则可以使用它使用regex或类似的东西将其更改为绝对路径,然后将其传递给FileInputStream I should make it clear that FileInputStream does need absolute paths for working. 我应该明确指出FileInputStream确实需要绝对路径才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM