- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create and store property file dynamically in Java?
The .propertiesis an extension in java which is used to store configurable application. It is represented by the Properties class in Java, you can store a properties file and read from it using the methods of this class. This class inherits the HashTable class.
Creating a .properties file
To create a properties file −
- Instantiate the Properties class.
- Populate the created Properties object using the put() method.
- Instantiate the FileOutputStream class by passing the path to store the file, as a parameter.
Example
The Following Java program creates a properties file in the path D:/ExampleDirectory/
import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile { public static void main(String args[]) throws IOException { //Instantiating the properties file Properties props = new Properties(); //Populating the properties file props.put("Device_name", "OnePlus7"); props.put("Android_version", "9"); props.put("Model", "GM1901"); props.put("CPU", "Snapdragon855"); //Instantiating the FileInputStream for output file String path = "D:\ExampleDirectory\myFile.properties"; FileOutputStream outputStrem = new FileOutputStream(path); //Storing the properties file props.store(outputStrem, "This is a sample properties file"); System.out.println("Properties file created......"); } }
Output
Properties file created......
If you observe the output file you can see the created contents as −
Storing properties file in XML format
You can store the properties file in XMLformat using the storeToXML() method.
Example
import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class CreatingPropertiesFile { public static void main(String args[]) throws IOException { //Instantiating the properties file Properties props = new Properties(); //Populating the properties file props.put("Device_name", "OnePlus7"); props.put("Android_version", "9"); props.put("Model", "GM1901"); props.put("CPU", "Snapdragon855"); //Instantiating the FileInputStream for output file String outputPath = "D:\ExampleDirectory\myFile.xml"; FileOutputStream outputStrem = new FileOutputStream(outputPath); //Storing the properties file in XML format props.storeToXML(outputStrem, "This is a sample properties file"); System.out.println("Properties file created......"); } }
Output
Properties file created......
If you observe the output file you can see the created contents as −
- Related Articles
- How to load a JavaScript file Dynamically?
- How to store the contents of arrays in a file using Java?
- How to create arrays dynamically in C#?
- How to create a pdf file in Java?
- How to create and write JSON array to a file in java?
- How to fetch a property value dynamically in C#?
- How to store list in a txt file and read list from txt file in android?
- How to create div dynamically using jQuery?
- How to change variables in the .env file dynamically in Laravel?
- MongoDB query to store File Name and location?
- How to create FileFilter for JFileChooser in Java and display File Type accordingly?
- How to create a Feature file for Cucumber in Java?
- How to Write/create a JSON file using Java?
- How to change JButton font dynamically in Java?
- How to create Directories using the File utility methods in Java?

Advertisements