Reputation: 121
I tried to read a .properties file in Java 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 it keeps saying file not found.
The content of properties is
pwd=passw0rd
Anyone knows how to solve this problem?
Upvotes: 12
Views: 29800
Reputation: 430
You can have two choices of selecting the path,
Open the file containing folder and get the path and save that path in the string with file like,
InputStream propertiesStream = Object.class.getResourceAsStream(path + File.seperator + "config.properties");
Save the file in src path,
WorkSpace -> Project Name -> Copyhere
Upvotes: -1
Reputation: 9295
You could also keep config.properties in the same folder as Config.java.
//InputStream propertiesStream = Object.class.getResourceAsStream("config.properties");
InputStream propertiesStream = Config.class.getResourceAsStream("config.properties");
Upvotes: -1
Reputation: 1029
it should be in WebContent/Web-Inf/
folder
and in you xml file define the bean like this :
<bean id="propertyConfigurer" class="com.your.project.util.properties.ApplicationProperties">
<property name="locations">
<list>
<value>/WEB-INF/application.properties</value>
</list>
</property>
</bean>
Upvotes: 0
Reputation: 240976
It should be in classpath, put it to your root source package, if its a maven project put it to src/main/resources
directory
Upvotes: 23