Java – where to place the properties file in eclipse and how to access the path locally and on the server

I see some similar questions here, but I don't know much about them, so I want to ask questions and customize them in my own way

I have a property file. I store it in Src / main / resources, and then reference it in my code, as shown below:

Properties prop = new Properties();
    InputStream input = null;

    try { 
        input = new FileInputStream("src/main/resources/app.properties");

        // load a properties file
        prop.load(input);

This works locally, but when I create it When the war file is and deployed to the server, it does not know where the properties file is Where do I place this property file and how do I reference it in the code so that I can run it locally and on the server without any problems?

Solution

I assume you have a maven project, and you can put the properties file in Src / main / resources / APP Properties and change the code to read it as

getClass().getClassLoader().getResourceAsStream("/app.properties");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>