diff --git a/core-java/src/main/java/com/baeldung/properties/DefaultListDemo.java b/core-java/src/main/java/com/baeldung/properties/DefaultListDemo.java new file mode 100644 index 0000000000..758bd2a71b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/properties/DefaultListDemo.java @@ -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")); + } +} diff --git a/core-java/src/main/java/com/baeldung/properties/LoadDemo.java b/core-java/src/main/java/com/baeldung/properties/LoadDemo.java new file mode 100644 index 0000000000..3a75113b79 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/properties/LoadDemo.java @@ -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)); + } +} diff --git a/core-java/src/main/java/com/baeldung/properties/OperationsDemo.java b/core-java/src/main/java/com/baeldung/properties/OperationsDemo.java new file mode 100644 index 0000000000..c33caf877c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/properties/OperationsDemo.java @@ -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 valueEnumeration = appProps.elements(); + while (valueEnumeration.hasMoreElements()) { + System.out.println(valueEnumeration.nextElement()); + } + + // get an enumeration of all keys + Enumeration 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); + } +} diff --git a/core-java/src/main/java/com/baeldung/properties/StoreDemo.java b/core-java/src/main/java/com/baeldung/properties/StoreDemo.java new file mode 100644 index 0000000000..e8a7892c40 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/properties/StoreDemo.java @@ -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"); + } +} diff --git a/core-java/src/main/resources/app.properties b/core-java/src/main/resources/app.properties new file mode 100644 index 0000000000..ff6174ec2a --- /dev/null +++ b/core-java/src/main/resources/app.properties @@ -0,0 +1,3 @@ +version=1.0 +name=TestApp +date=2016-11-12 \ No newline at end of file diff --git a/core-java/src/main/resources/catalog b/core-java/src/main/resources/catalog new file mode 100644 index 0000000000..488513d0ce --- /dev/null +++ b/core-java/src/main/resources/catalog @@ -0,0 +1,3 @@ +c1=files +c2=images +c3=videos \ No newline at end of file diff --git a/core-java/src/main/resources/default.properties b/core-java/src/main/resources/default.properties new file mode 100644 index 0000000000..df1bab371c --- /dev/null +++ b/core-java/src/main/resources/default.properties @@ -0,0 +1,4 @@ +site=www.google.com +name=DefaultAppName +topic=Properties +category=core-java \ No newline at end of file diff --git a/core-java/src/main/resources/icons.xml b/core-java/src/main/resources/icons.xml new file mode 100644 index 0000000000..0db7b2699a --- /dev/null +++ b/core-java/src/main/resources/icons.xml @@ -0,0 +1,8 @@ + + + + xml example + icon1.jpg + icon2.jpg + icon3.jpg + \ No newline at end of file