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:-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- Fix SharePoint Error - The Managed Metadata Service or Connection is currently not available. The Application Pool or Managed Metadata Web Service may not have been started - SharePoint
- Outlook and Hotmail not working - email issue, message not delivered, send receive problem - Microsoft
- Set width and height for the label in tkinter - Python
- How to install zsh shell on Ubuntu - Ubuntu
- AlertDialog with image using ImageView Example - Android
- Bash - How to check if a Command Failed? - Bash
- Android Studio: Cannot perform refactoring operation - Android-Studio
- MainActivity error: cannot find symbol FloatingActionButton - Android