• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Reading properties file from java

May 25, 2011 By j2eereference Leave a Comment

What is properties file?

A Properties file is used to store parameters and its values. Properties file has the extension .properties
Each parameter is stored as a pair of strings, one storing the name of the parameter called the key, and the other storing the value.

How to create a Properties file?

You can create properties file using any text editor and save the file with the extension .properties

How to read properties file in java

Here , you will learn how to read the key-value of properties files using Java with a simple example. Let’s consider properties file named sample.properties and which has some parameters like name and age .we can perform the following steps to read the values of these parameters.

Steps 1

Let us create a properties file called sample.properties , which has the following content and put it in our application root folder.

NAME=John
AGE=30

Step 2:

Write the java code to read this properties file and get the value of the parameter website

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
 
public class ReadPropertiesFile {
public static void main(String[] args) {
FileInputStream in = null;
Properties props = new Properties();
try {
in = new FileInputStream(new File("sample.properties"));
props.load(in);
String name = props.getProperty("NAME");
String age = props.getProperty("AGE");
System.out.println("Name is - " + name);
System.out.println("Age is - " + age);
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Output:

Name is – John

Age is – 30

 Getting all the keys defined in a properties file

The method propertyNames() will give you the list of all keys defined in the properties file and this method will return the Enumeration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;
 
public class ReadPropertiesFile {
public static void main(String[] args) {
FileInputStream in = null;
Properties props = new Properties();
try {
in = new FileInputStream(new File("sample.properties"));
props.load(in);
Enumeration enumeration = (Enumeration) props.propertyNames();
while (enumeration.hasMoreElements()) {
System.out.println(" Key: " + enumeration.nextElement());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

The above code will display all the keys defined in the sample.properties file and the output is

Key: NAME

Key: AGE

 

Related Posts

  • Busy Spinning in mutithreading
  • Implementing queue in java
  • TreeSet vs ConcurrentSkipListSet
  • How to create immutable class?
  • Implementing Stack in java
  • What is ReentrantLock?
  • What is Semaphore in java
  • Why AtomicInteger class
  • What is CyclicBarrier?
  • CountDownLatch in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.