Properties File Writing Example
This program demonstrates writing properties into a .properties file.
Problem Statement
Write a Java program that writes key-value pairs into a properties file.
Source Code
| 1 | import java.io.*; |
| 2 | import java.util.*; |
| 3 | public class WritePropertiesExample { |
| 4 | public static void main(String[] args) { |
| 5 | System.out.println("Eduinq Write Properties Example"); |
| 6 | Properties p=new Properties(); |
| 7 | p.setProperty("username","eduinq"); |
| 8 | p.setProperty("password","secure123"); |
| 9 | try(FileWriter fw=new FileWriter("config.properties")) { |
| 10 | p.store(fw,"Eduinq Configurations"); |
| 11 | System.out.println("Eduinq Properties Written Successfully"); |
| 12 | } catch(IOException e) { |
| 13 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 14 | } |
| 15 | } |
| 16 | } |
Program Output
Eduinq Write Properties Example Eduinq Properties Written Successfully
Explanation
Properties writing uses Properties.store(). - Stores key-value pairs in a file. - Exception handling ensures safe operations. - In enterprise systems, properties writing is used for dynamic configuration. Conclusion: Shows writing properties files.