core-java Properties class demos

This commit is contained in:
Diane Duan 2016-11-14 14:53:12 +08:00
parent aeb8f7595c
commit dd6946585f
8 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class DefaultListDemo {
public static void main(String[] args) throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String defaultConfigPath = rootPath + "default.properties";
String appConfigPath = rootPath + "app.properties";
Properties defaultProps = new Properties();
defaultProps.load(new FileInputStream(defaultConfigPath));
Properties appProps = new Properties(defaultProps);
appProps.load(new FileInputStream(appConfigPath));
System.out.println(appProps.getProperty("name"));
System.out.println(appProps.getProperty("version"));
System.out.println(appProps.getProperty("site"));
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class LoadDemo {
public static void main(String[] args) throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
String catalogConfigPath = rootPath + "catalog";
String iconConfigPath = rootPath + "icons.xml";
// load from properties file which use .properties as the suffix
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
// load from properties file which not use .properties as the suffix
Properties catalogProps = new Properties();
catalogProps.load(new FileInputStream(catalogConfigPath));
// load from XML file
Properties iconProps = new Properties();
iconProps.loadFromXML(new FileInputStream(iconConfigPath));
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class OperationsDemo {
public static void main(String[] args) throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
// retrieve values
String appVersion = appProps.getProperty("version");
String appName = appProps.getProperty("name", "defaultName");
String appGroup = appProps.getProperty("group", "baeldung");
String appDownloadAddr = appProps.getProperty("downloadAddr");
System.out.println(appVersion);
System.out.println(appName);
System.out.println(appGroup);
System.out.println(appDownloadAddr);
// set values
appProps.setProperty("name", "NewAppName");
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
appName = appProps.getProperty("name");
appDownloadAddr = appProps.getProperty("downloadAddr");
System.out.println("new app name: " + appName);
System.out.println("new app downloadAddr: " + appDownloadAddr);
// remove a key-value pair
System.out.println("before removal, version is: " + appProps.getProperty("version"));
appProps.remove("version");
System.out.println("after removal, version is: " + appProps.getProperty("version"));
// list all key-value pairs
appProps.list(System.out);
// get an enumeration of all values
Enumeration<Object> valueEnumeration = appProps.elements();
while (valueEnumeration.hasMoreElements()) {
System.out.println(valueEnumeration.nextElement());
}
// get an enumeration of all keys
Enumeration<Object> keyEnumeration = appProps.keys();
while (keyEnumeration.hasMoreElements()) {
System.out.println(keyEnumeration.nextElement());
}
// get the count of key-value pairs
int size = appProps.size();
System.out.println(size);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.properties;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class StoreDemo {
public static void main(String[] args) throws IOException {
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
String appConfigPath = rootPath + "app.properties";
// load the original properties
Properties appProps = new Properties();
appProps.load(new FileInputStream(appConfigPath));
// update property list
appProps.setProperty("name", "NewAppName");
appProps.setProperty("downloadAddr", "www.baeldung.com/downloads");
// store new property list in properties file
String newAppConfigPropertiesFile = rootPath + "newApp.properties";
appProps.store(new FileWriter(newAppConfigPropertiesFile), "store to properties file");
// store new property list in XML file
String newAppConfigXmlFile = rootPath + "newApp.xml";
appProps.storeToXML(new FileOutputStream(newAppConfigXmlFile), "store to xml file");
}
}

View File

@ -0,0 +1,3 @@
version=1.0
name=TestApp
date=2016-11-12

View File

@ -0,0 +1,3 @@
c1=files
c2=images
c3=videos

View File

@ -0,0 +1,4 @@
site=www.google.com
name=DefaultAppName
topic=Properties
category=core-java

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>xml example</comment>
<entry key="fileIcon">icon1.jpg</entry>
<entry key="imageIcon">icon2.jpg</entry>
<entry key="videoIcon">icon3.jpg</entry>
</properties>