The Properties file in Java programming is a file that is used to store key-value pair Strings that mainly contain configuration details. The extension of this file is .properties
Example: Write a .properties file in Java using Properties class
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
/**
* Example: Write properties file.
*/
public class WriteJavaPropertiesFile {
public static void main(String... args) throws IOException {
OutputStream outputStream = new FileOutputStream("config.properties");
Properties properties = new Properties();
//Production Environment
properties.setProperty("prod.db.username","root");
properties.setProperty("prod.db.password","123456");
properties.setProperty("prod.domain","https://code2care.org");
//UAT Environment
properties.setProperty("uat.db.username","root");
properties.setProperty("uat.db.password","654321");
properties.setProperty("uat.domain","https://uat.code2care.org");
properties.store(outputStream, "This is our prop file!");
}
}
Example: Read a .properties file in Java using Properties class
package org.code2care;
import java.io.*;
import java.util.Properties;
class ReadJavaPropertiesFile {
public static void main(String... args) throws IOException {
InputStream inputStream = new FileInputStream("config.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("uat.db.password"));
System.out.println(properties.getProperty("prod.db.password"));
System.out.println(properties.getProperty("prod.domain"));
}
}
Output:
654321
123456
https://code2care.org
Exception:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
/**
* Example: Write properties file.
*/
public class WriteJavaPropertiesFile {
public static void main(String... args) throws IOException {
OutputStream outputStream = new FileOutputStream("config.properties");
Properties properties = new Properties();
//Production Environment
properties.setProperty("prod.db.username","root");
properties.setProperty("prod.db.password","123456");
properties.setProperty("prod.domain","https://code2care.org");
//UAT Environment
properties.setProperty("uat.db.username","root");
properties.setProperty("uat.db.password","654321");
properties.setProperty("uat.domain","https://uat.code2care.org");
properties.store(outputStream, "This is our prop file!");
}
}
package org.code2care;
import java.io.*;
import java.util.Properties;
class ReadJavaPropertiesFile {
public static void main(String... args) throws IOException {
InputStream inputStream = new FileInputStream("config.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("uat.db.password"));
System.out.println(properties.getProperty("prod.db.password"));
System.out.println(properties.getProperty("prod.domain"));
}
}
654321
123456
https://code2care.org
If the file is not present, you will get FileNotFoundException,
Exception in thread "main" java.io.FileNotFoundException: configs.properties (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at org.code2care.ReadJavaPropertiesFile.main(ReadJavaProperties.java:10)
If a property is not found then the getProperty() returned value is null.
Get Code:Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- How to Install Windows Terminal Without the Store - Windows
- 3 ways to clear screen on Linux Terminal - Linux
- Unable to establish connection to adb : Android Studio Error - Android
- How to Change Android Title Bar Color? - Android
- Change Google Chrome Browsers default download location - Chrome
- How to install MySQL Workbench on Mac (M1/M2) - MySQL
- How to create an Autocomplete Android EditText Example - Android
- 30: How to Check if a Directory Exists or Not in Python Program - Python