I am new to the Java. I want to know what is a Java Property Category?Can someone explain me by giving an example. Also tell me the purpose of using them.? Since I am new to the Java, very simple and right answers are warmly welcomed.!
I am new to the Java. I want to know what is a Java Property Category?Can someone explain me by giving an example. Also tell me the purpose of using them.? Since I am new to the Java, very simple and right answers are warmly welcomed.!
Signatures reduce available bandwidth
I think that you want to know about the Java Property class. To save and restore the properties for a program of Java, the function of the java.util.Properties class is used. Also using the java.util.Preferences will be easier. The java.util.Properties class stores and loads key/value pairs from a file, and manages them in memory. I am giving an example code for creating a new Properties table and I am assigning a string value to three different keys. Here is the code for that :
Code:Properties props = new Properties(); props.setProperty("recursiveSearch", "true"); props.setProperty("maxLevel", "7"); props.setProperty("fileName", "C:\temp\work.html");
You can use the Properties .store method for storing a Properties table in a file. This method will write the properties to the output stream, with a header line from the String parameter.
Code:boolean recursiveSearch; String noCopyPattern; int maxLevel; . . . try { OutputStream propOut = new FileOutputStream( new File("props.stat")); }catch (. . .
I think that you are asking about the System Properties. I have explained how to get the System Properties. You can get only one property at a time by supplying the key in a call to System.getProperty().
- String System.getProperty(String key, String default) - This property Returns the value of property key as a string.
- String System.getProperty(String key) - This property returns the value of property key as a String.
- Properties System.getProperties() - This property returns a Properties object which has the value of all the properties.
You can use the following program for displaying a list of System Properties.
Code:import java.awt.*; import javax.swing.*; import java.util.*; public class SysPropList { public static void main(String[] args) { JFrame window = new JFrame("System Properties"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setContentPane(new SysPropListGUI()); window.pack(); window.setVisible(true); } } class SysPropListGUI extends JPanel { JTextArea m_propertiesTA = new JTextArea(20, 40); public SysPropListGUI() { this.setLayout(new BorderLayout()); this.add(new JScrollPane(m_propertiesTA), BorderLayout.CENTER); Properties pr = System.getProperties(); TreeSet propKeys = new TreeSet(pr.keySet()); // TreeSet sorts keys for (Iterator it = propKeys.iterator(); it.hasNext(); ) { String key = (String)it.next(); m_propertiesTA.append("" + key + "=" + pr.get(key) + "\n"); } } }
Bookmarks