How to use Properties

How to use Properties

Question:
I’m trying to set and save user preferences for an application to a local file. The java.util.Properties class looks like what I want, but I don’t understand how to use it.

Answer:
The Properties class is a Hashtable intended only to store strings. Rather than use the Hashtable put method, you use setProperty(String key, String value). To remove a key, you have to use the Hashtable remove(Stringkey) method. The Properties class not only allows you to set and remove properties, but it also allows you to load a setof properties from an InputStream, save them to an OutputStream, and print them to a PrintStream. The load(InputStream stream) method loads a properties file, store(OutputStream output, String header) saves the properties, and list(PrintStream out) lists them. Pretty easy, eh? Thefollowing example demonstrates how to use all of these features, enabling you to create a properties file, add keys-value pairs, deletekeys, and list the properties.

import java.util.*;import java.io.*;/*** * This program demonstrates how to use the Properties class.  It uses * some methods added in JDK 1.2 and will not work with JDK 1.1.  You * can add a key-value pair to a properties file using * 
 *          java PropertiesExample filename set key value * 

* and delete a key using *

 *          java PropertiesExample filename del key * 

* and list the property file using *

 *          java PropertiesExample filename list * 

* If the file does not exist when you add a key-value pair, it is created. ***/public class PropertiesExample { public static final int SET = 1; public static final int DEL = 2; public static final int LIST = 3; public static void printUsage() { System.err.println( "Usage: PropertiesExample filename list|set|del [key] [value]"); } public static void main(String[] args) { File file; Properties properties; String filename, key = null, value = null; InputStream input; OutputStream output; int action; if(args.length < 1) { printUsage(); return; } filename = args[0]; file = new File(filename); try { if(args[1].equals("list")) { action = LIST; } else if(args[1].equals("set")) { action = SET; if(args.length < 4) { printUsage(); return; } if(!file.exists()) { System.out.println(filename + " does not exist. Creating file."); file.createNewFile(); } key = args[2]; value = args[3]; } else if(args[1].equals("del")) { action = DEL; if(args.length < 3) { printUsage(); return; } if(!file.exists()) { System.err.println(filename + " does not exist!"); return; } key = args[2]; } else { printUsage(); return; } properties = new Properties(); properties.load(input = new FileInputStream(file)); input.close(); switch(action) { case SET: properties.setProperty(key, value); break; case DEL: properties.remove(key); break; case LIST: properties.list(System.out); break; } if(action != LIST) { properties.store(output = new FileOutputStream(file), null); output.close(); } } catch(IOException e) { e.printStackTrace(); return; } }}

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing