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:
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:-
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- Get Device Screen Width and Height using javaScript - JavaScript
- Comparator with Lambda Examples - Java
- How to take a photo on MacBook using Terminal Command? - MacOS
- How to connect Airpods to Macbook Air or Pro? - MacOS
- How to Remove All Terminated Console tabs at once in Eclipse - Eclipse
- [Solution] Installing Whatsapp There's insufficient space on the device - WhatsApp
- How to create tar.gz file using Terminal Command - Linux
- Open Current Directory in Finder using Mac Terminal - MacOS