Obtain data from a properties file in Eclipse

Home > Search
  by

Let's say you have a file named example.properties that contains the following markup.

greeting=Hello World

 

To obtain "Hello World" from example.properties, you can create a class. In the class, import the following modules.

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

 

Add the following markup to your class. Notice the the full path to the example.properties file is used. In this example, the example.properties file resides on the same system as Eclipse. The "greeting" key is obtained from example.properties, which will return a value of Hello World.

public class Hello {	
	Properties prop = new Properties();
	public String greeting;

	public Hello() {
 		InputStream fis = new FileInputStream("/opt/properties/example.properties");
		prop.load(fis);
			
		greeting = prop.getProperty("greeting");
			
		fis.close();
	} 
}

 

You can then do something with the value obtained from the example.properties file. For example, you could print greeting key, which is Hello World.

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments