Where to put the properties file?

I tried to read the .properties file and have the following code:

public final class Config { static { Properties properties = new Properties(); InputStream propertiesStream = Object.class.getResourceAsStream("config.properties"); if (propertiesStream != null) { try { properties.load(propertiesStream); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("file not found"); } } } 

but he continues to say that the file was not found.

Property Content

 pwd=passw0rd 

Does anyone know how to solve this problem?

+6
source share
4 answers

It should be in the classpath, placed in the root source package if its maven project places it in the src/main/resources directory

+15
source

it should be in the folder WebContent/Web-Inf/

and in your xml file define a bean as follows:

 <bean id="propertyConfigurer" class="com.your.project.util.properties.ApplicationProperties"> <property name="locations"> <list> <value>/WEB-INF/application.properties</value> </list> </property> </bean> 
0
source

You can also save config.properties in the same folder as Config.java.

 //InputStream propertiesStream = Object.class.getResourceAsStream("config.properties"); InputStream propertiesStream = Config.class.getResourceAsStream("config.properties"); 
0
source

You can have two options for choosing a path,

  • Open the file containing the folder and find the path and save this path in the line with the file, for example,

    Properties InputStreamStream = Object.class.getResourceAsStream (path + File.seperator + "config.properties");

  • Save the file in the src path,

    WorkSpace → Project Name → Copyhere

0
source

Source: https://habr.com/ru/post/946055/


All Articles