How do I read a configuration file using java.util.Properties?
Category: java.util, viewed: 1408 time(s).
When we have an application that used a text file to store a configuration and the configuration is typically in a "key"="value" format then we can use java.util.Properties to read that configuration file.
Here is the sample of app.config file:
app.name=Properties Sample Code app.version=1.0
The code below show you how to read the configuration.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.io.IOException; import java.util.Properties; public class PropertiesExample { public static void main(String[] args) { Properties prop = new Properties(); try { // the configuration file name String fileName = "app.config"; InputStream is = new FileInputStream(fileName); // load the properties file prop.load(is); // get the value for app.name key System.out.println(prop.getProperty("app.name")); // get the value for app.version key System.out.println(prop.getProperty("app.version")); // get the value for app.vendor key and if the // key is not available return Kode Java as // the default value System.out.println(prop.getProperty("app.vendor", "Kode Java")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!
Related Examples
- How do I convert LinkedList to array?
- How do I get all available timezones?
- How do I get currency symbol?
- How do I sort items in a Set?
- How do I sort items of an ArrayList?
- How do I sort array values in case insensitive order?
- How do I sort array values in descending order?
- How do I convert milliseconds value to date?
- How do I split a string using Scanner class?
- How do I read file using Scanner class?
- How do I read / write data in Windows registry?
- How do I read user input from console using Scanner class?
- How do I use ResourceBundle for i18n?
- How do I convert time between timezone?
- How do I set a default Locale?
- How do I remove duplicate element from array?
- How do I convert array to Set?
- How do I use the HashMap class?
- How do I generate a random array of numbers?
- How do I create a queue using LinkedList class?
Most Viewed Examples
Latest Code Examples
Categories
100 Top & Latest
Latest Jobs
Blog Entries
Forums Entries
- Google Chrome
- Re: Importing and scanning the text
- Re: What Java books have you read?
- Re: How to create exe file
- How to create exe file
- Re: What Java books have you read?
- Infix expression
- Importing and scanning the text
- Re: I want to reduce the size of a JPG image
- MOVED: I want to reduce the size of a JPG image
