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.(FileInputStream.java:157)
at java.base/java.io.FileInputStream.(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:More Posts related to Java,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- Display full website address in Safari macOS Browser - MacOS
- How to clear mysql console screen - MySQL
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- Disable Fading Edges Scroll Effect Android Views - Android
- The default interactive shell is now zsh. [macOS] - MacOS
- Fibonacci series from 1 to 500 table - Html
- Microsoft Teams adds New Conversation Button - Teams
- reCaptcha Verification expired. Check the checkbox again - Html