net.viralpatel.resources
. net/viralpatel/resources/config.properties
To load properties file using Classloader, use following code:Code language: Java (java)hello.world=Hello World
this.getClass()
.getResourceAsStream("/some/package/config.properties");
Code language: Java (java)
The Class.getResourceAsStream(name) returns an Inputstream for a resource with a given name or null if no resource with the given name is found. The name of a resource is a ‘/’-seperated path name that identifies the resource. If the name with a leading “/” indicates the absolute name of the resource is the portion of the name following the ‘/’. In Class.getResourceAsStream(name), the rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object’s class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String). So in our example to load config.properties we will have a method loadProps1()
. private Properties configProp = new Properties();
...
public void loadProps1() {
InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties");
try {
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
Code language: Java (java)
To load properties file using Classloader, use following code: this.getClass()
.getClassLoader()
.getResourceAsStream("some/package/config.properties");
Code language: Java (java)
The ClassLoader.getResourceAsStream(name) returns an Inputstream for reading the specified resource or null if the resource could not be found. The name of a resource is a ‘/’-seperated path name that identifies the resource. The name no leading ‘/’ (all namea are absolute). So in our example to load config.properties we will have a method loadProps2()
. private Properties configProp = new Properties();
public void loadProps2() {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties");
try {
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
Code language: Java (java)
The folder structure of our example code will be: package net.viralpatel.java;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
public class LoadPropertiesExample {
private Properties configProp = new Properties();
public static void main(String[] args) {
LoadPropertiesExample sample = new LoadPropertiesExample();
sample.loadProps2();
sample.sayHello();
}
public void loadProps1() {
InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties");
try {
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadProps2() {
InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties");
try {
configProp.load(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sayHello() {
System.out.println(configProp.getProperty("hello.world"));
}
}
Code language: Java (java)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Such a long article for so simple thing.
Interesante aqui te dejo mis dos Blogs. Aqui encontraras cosas tambien muy interesantes:
http://viviendoconjavaynomoririntentandolo.blogspot.com
http://frameworksjava2008.blogspot.com
Saludos.
Asking the class itself for resources seems weird to me. Is there any way to get that by asking the System or by other API not connected to the class you are using it in?
Hi..
To load the property file in Java I think, it is very easy to do it. It will come in the form of directory when you are downloading this files..
In Java 6 updated version, every properties class have been existed, take help form there to fulfil the requirements....
Simple method to load properties file.
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
String value = props.getProperty("prop1");
System.out.println(value);
System.out.println(props.entrySet());
Thank you Muski .. useful one :)
Thanks man,
awesome post.
helped me in reading the properties file outside the jar file.
Thanks again
Hi Viral,
NIce post. HOw do store some modified properties to the file in the same package as you mentioned. I used FileOutputstream, but it creates a new properties file elsewhere..i want the same properties file to contain the modified values...i am goign berserk with this...
Thanks Dude
Very good Post.
I was struggling like anything.
My problem was to load dataset file from resource folder (in DBUnit)
return new FlatXmlDataSetBuilder().build(this.getClass().getResourceAsStream("/dataset.xml"));
thanks again;
Could you tell me how to include the properties file path in my manifest? my properties file is in the resources folder. I need to include this in the class path of the JAR file, so that if any user wants they can modify the properties file externally and it will reflect in the JAR file
Very good, thanks