🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the Properties load() method in Java programming and how to use it with an example.
1. Properties load() Method Overview
Definition:
The load() method of the Properties class in Java is used to read a property list (key and element pairs) from the input stream. This method is part of the java.util.Properties class, which is a subclass of Hashtable and represents a persistent set of properties that can be loaded from or saved to a stream.
Syntax:
public void load(InputStream inStream) throws IOException
public void load(Reader reader) throws IOException
Parameters:
- inStream: The input stream from which the properties are loaded.
- reader: A character input stream.
Key Points:
- The load() method is used to load properties from an input stream or a reader.
- It reads the properties in the form of key-value pairs.
- The method throws an IOException if reading from the specified input stream or reader fails.
- Any malformed Unicode escape sequences encountered in the input will cause an IllegalArgumentException to be thrown.
- The properties are usually stored in a file with the .properties extension, but they can also be loaded from other types of streams.
2. Properties load() Method Example
import java.io.StringReader;
import java.util.Properties;
public class PropertiesLoadExample {
public static void main(String[] args) {
try {
Properties properties = new Properties();
String propertiesString = "key1=value1\nkey2=value2";
StringReader reader = new StringReader(propertiesString);
// Loading properties from a string using load() method
properties.load(reader);
// Displaying the loaded properties
System.out.println("Loaded Properties: " + properties);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
Loaded Properties: {key1=value1, key2=value2}
Explanation:
In this example, a Properties object is created, and a StringReader is initialized with a string containing property key-value pairs. The load() method is then used to load these properties. The output displays the loaded properties, verifying that the properties have been successfully loaded into the Properties object from the given string.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment