Java by API/java.util/Properties
Содержание
- 1 new Properties()
- 2 new Properties(Properties prop)
- 3 Properties: getProperty(String key)
- 4 Properties: getProperty(String key, String defaultValue)
- 5 Properties: keySet()
- 6 Properties: list(PrintStream out)
- 7 Properties: loadFromXML(InputStream in)
- 8 Properties: load(InputStream inStream)
- 9 Properties: propertyNames()
- 10 Properties: setProperty(String key, String value)
- 11 Properties: store(OutputStream out, String comments)
- 12 Properties: storeToXML(OutputStream os, String comment)
new Properties()
/*
* Output:
*
a 1
A 2
c: 3
z: default
*
*/
import java.util.Properties;
public class MainClass {
public static void main(String args[]) {
Properties prop = new Properties();
prop.put("a", "1");
prop.put("b", "2");
prop.put("c", "3");
Properties book = new Properties(prop);
book.put("A", "4");
book.put("B", "5");
System.out.println("a " + book.getProperty("a"));
System.out.println("A " + book.getProperty("b"));
System.out.println("c: " + book.getProperty("c"));
System.out.println("z: " + book.getProperty("z", "default"));
}
}
new Properties(Properties prop)
/*
* Output:
*
a 1
A 2
c: 3
z: default
*
*/
import java.util.Properties;
public class MainClass {
public static void main(String args[]) {
Properties prop = new Properties();
prop.put("a", "1");
prop.put("b", "2");
prop.put("c", "3");
Properties book = new Properties(prop);
book.put("A", "4");
book.put("B", "5");
System.out.println("a " + book.getProperty("a"));
System.out.println("A " + book.getProperty("b"));
System.out.println("c: " + book.getProperty("c"));
System.out.println("z: " + book.getProperty("z", "default"));
}
}
Properties: getProperty(String key)
/*
* Output:
*
a 1
A 2
c: 3
z: default
*
*/
import java.util.Properties;
public class MainClass {
public static void main(String args[]) {
Properties prop = new Properties();
prop.put("a", "1");
prop.put("b", "2");
prop.put("c", "3");
Properties book = new Properties(prop);
book.put("A", "4");
book.put("B", "5");
System.out.println("a " + book.getProperty("a"));
System.out.println("A " + book.getProperty("b"));
System.out.println("c: " + book.getProperty("c"));
System.out.println("z: " + book.getProperty("z", "default"));
}
}
Properties: getProperty(String key, String defaultValue)
/*
* Output:
*
a 1
A 2
c: 3
z: default
*
*/
import java.util.Properties;
public class MainClass {
public static void main(String args[]) {
Properties prop = new Properties();
prop.put("a", "1");
prop.put("b", "2");
prop.put("c", "3");
Properties book = new Properties(prop);
book.put("A", "4");
book.put("B", "5");
System.out.println("a " + book.getProperty("a"));
System.out.println("A " + book.getProperty("b"));
System.out.println("c: " + book.getProperty("c"));
System.out.println("z: " + book.getProperty("z", "default"));
}
}
Properties: keySet()
/**
*Output:
The value of K2 is V2.
The value of K1 is V1.
*/
import java.util.Properties;
import java.util.Set;
public class MainClass {
public static void main(String args[]) {
Properties capitals = new Properties();
capitals.put("K1", "V1");
capitals.put("K2", "V2");
Set states = capitals.keySet();
for (Object name : states)
System.out.println("The value of " + name + " is "
+ capitals.getProperty((String) name) + ".");
}
}
Properties: list(PrintStream out)
/*
* Output:
-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
java.vm.version=1.5.0-b64
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.ru/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.5.0-b64
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
...
...
*/
import java.util.Properties;
public class MainClass {
public static void main(String[] args) {
Properties properties = System.getProperties();
properties.list(System.out);
}
}
Properties: loadFromXML(InputStream in)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;
public class Main {
public static void main(String args[]) throws Exception {
Properties p = new Properties();
p.put("today", new Date().toString());
p.put("user", "A");
FileOutputStream out = new FileOutputStream("user.props");
p.storeToXML(out, "updated");
FileInputStream in = new FileInputStream("user.props");
p.loadFromXML(in);
p.list(System.out);
}
}
Properties: load(InputStream inStream)
import java.io.FileInputStream;
import java.util.Properties;
public class MainClass {
public static void main(String args[]) throws Exception {
Properties p = new Properties();
p.load(new FileInputStream("colon.txt"));
p.list(System.out);
}
}
//File: colon.txt
/*
foo:bar
one
two
three=four
five six seven eight
nine ten
*/
Properties: propertyNames()
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Properties;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ToolTipManager;
public class MainClass extends JList {
SortedListModel model;
Properties tipProps;
public MainClass(Properties props) {
model = new SortedListModel();
setModel(model);
ToolTipManager.sharedInstance().registerComponent(this);
tipProps = props;
addProperties(props);
}
private void addProperties(Properties props) {
Enumeration names = props.propertyNames();
while (names.hasMoreElements()) {
model.add(names.nextElement());
}
}
public String getToolTipText(MouseEvent event) {
Point p = event.getPoint();
int location = locationToIndex(p);
String key = (String) model.getElementAt(location);
String tip = tipProps.getProperty(key);
return tip;
}
public static void main(String args[]) {
JFrame frame = new JFrame("Custom Tip Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Properties props = System.getProperties();
MainClass list = new MainClass(props);
JScrollPane scrollPane = new JScrollPane(list);
frame.add(scrollPane);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
class SortedListModel extends AbstractListModel {
SortedSet<Object> model;
public SortedListModel() {
model = new TreeSet<Object>();
}
public int getSize() {
return model.size();
}
public Object getElementAt(int index) {
return model.toArray()[index];
}
public void add(Object element) {
if (model.add(element)) {
fireContentsChanged(this, 0, getSize());
}
}
public void addAll(Object elements[]) {
Collection<Object> c = Arrays.asList(elements);
model.addAll(c);
fireContentsChanged(this, 0, getSize());
}
public void clear() {
model.clear();
fireContentsChanged(this, 0, getSize());
}
public boolean contains(Object element) {
return model.contains(element);
}
public Object firstElement() {
return model.first();
}
public Iterator iterator() {
return model.iterator();
}
public Object lastElement() {
return model.last();
}
public boolean removeElement(Object element) {
boolean removed = model.remove(element);
if (removed) {
fireContentsChanged(this, 0, getSize());
}
return removed;
}
}
Properties: setProperty(String key, String value)
import java.io.FileOutputStream;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("database.type", "mysql");
properties.setProperty("database.url", "jdbc:mysql://localhost/mydb");
properties.setProperty("database.username", "root");
properties.setProperty("database.password", "root");
FileOutputStream fos = new FileOutputStream("database-configuration.xml");
properties.storeToXML(fos, "Database Configuration", "UTF-8");
}
}
Properties: store(OutputStream out, String comments)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class Main {
public static void main(String args[]) throws Exception {
Properties p = new Properties();
p.load(new FileInputStream("test.txt"));
p.store(new FileOutputStream("t.txt"),"no comments");
}
}
Properties: storeToXML(OutputStream os, String comment)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.Properties;
public class Main {
public static void main(String args[]) throws Exception {
Properties p = new Properties();
p.put("today", new Date().toString());
p.put("user", "A");
FileOutputStream out = new FileOutputStream("user.props");
p.storeToXML(out, "updated");
FileInputStream in = new FileInputStream("user.props");
p.loadFromXML(in);
p.list(System.out);
}
}