Friday 10 March 2017

Java Tutorial: Java properties [How to print the property list out to the specified print writer]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=Vg30T1Yg7eo&list=UUhwKlOVR041tngjerWxVccw

db.properties
user=root 
password=oracle 

PropertiesDemo.java
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

/*
 * public void list(PrintWriter out)
 * 
 * Parameters:
 * ----------
 * 
 * out - an output stream.
 */
class PropertiesDemo
{

    public static void main(String[] args) throws IOException
    {

        try (FileReader fileReader = new FileReader("db.properties");)
        {

            Properties p = new Properties();
            /*
             * Reads a property list (key and element pairs)
             * from the input character stream in a simple
             * line-oriented format.
             */
            p.load(fileReader);

            PrintWriter writer = new PrintWriter(System.out);
            /*
             * print the list with a PrintWriter object
             */
            p.list(writer);

            /*
             * flush the stream
             */
            writer.flush();

        }

    }
}
Output
-- listing properties --
user=root 
password=oracle 

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_list_pw_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_list_pw_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2f1fa585daa61fc8fa0957d6f04b940eddbcf7ec/BasicJava/PropertiesDemo_list_pw_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial: Java properties [How to print the property list out to the specified output stream]


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=YJsQppAzi9A&list=UUhwKlOVR041tngjerWxVccw

    db.properties
    user=root 
    password=oracle 
    
    
    PropertiesDemo.java
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Properties;
    
    /*
     * public void list(PrintStream out)
     * 
     * Parameters:
     * ----------
     * 
     * out - an output stream.
     */
    class PropertiesDemo
    {
    
        public static void main(String[] args) throws IOException
        {
    
            try (FileReader fileReader = new FileReader("db.properties");)
            {
    
                Properties p = new Properties();
                /*
                 * Reads a property list (key and element pairs)
                 * from the input character stream in a simple
                 * line-oriented format.
                 */
                p.load(fileReader);
    
                /*
                 * Prints this property list out to the
                 * specified output stream.This method is useful
                 * for debugging.
                 */
                p.list(System.out);
    
            }
    
        }
    }
    
    Output
    -- listing properties --
    user=root 
    password=oracle 
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_list_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_list_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_list_App/?at=master

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Monday 6 March 2017

    Java Tutorial: Properties class in java [How to store key-value pairs in xml]


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=9QvNyqi2xGs&list=UUhwKlOVR041tngjerWxVccw

    PropertiesDemo.java
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    /*
     * public void storeToXML(OutputStream os, String
     *                              comment) throws IOException
     * 
     * Parameters: 
     * -----------
     * 
     * os - the output stream on which to emit the XML
     * document. 
     * 
     * comment - a description of the property list, 
     * or null if no comment is desired.
     */
    
    class PropertiesDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            Properties p = new Properties();
            // add some properties
            p.put("name", "Dog");
            p.put("age", "2 years");
    
            try (FileOutputStream fos = new FileOutputStream("properties.xml"))
            {
                /*
                 * Emits an XML document representing all of the
                 * properties contained in this table.
                 */
                p.storeToXML(fos, "Animal properties");
                System.out.println("Xml file is created successfully..");
    
            }
    
        }
    }
    
    Output
    Xml file is created successfully..
    
    
    properties.xml
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
    <comment>Animal properties</comment>
    <entry key="age">2 years</entry>
    <entry key="name">Dog</entry>
    </properties>
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_storeToXML_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_storeToXML_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_storeToXML_App/?at=master

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial: Properties class in java[How to load key-value pairs from xml]


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=vKZFsTAoGw8&list=UUhwKlOVR041tngjerWxVccw

    properties.xml
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
    <entry key="Width">15</entry>
    <entry key="Height">200</entry>
    </properties>
    
    
    PropertiesDemo.java
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.Properties;
    
    /*
     * public void loadFromXML(InputStream in) throws
     *                      IOException, InvalidPropertiesFormatException
     * 
     * Parameters: 
     * -----------
     * 
     * in - the input stream from which to read the XML
     * document.
     */
    class PropertiesDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            Properties p = new Properties();
    
            try (FileInputStream fis = new FileInputStream("properties.xml"))
            {
                /*
                 * Loads all of the properties represented by
                 * the XML document on the specified input
                 * stream into this properties table.
                 */
                p.loadFromXML(fis);
    
                Enumeration<?> enumeration = p.propertyNames();
    
                while (enumeration.hasMoreElements())
                {
                    String key = (String) enumeration.nextElement();
                    String value = p.getProperty(key);
    
                    System.out.println(key + " = " + value);
                }
    
            }
    
        }
    }
    
    Output
    Width = 15
    Height = 200
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_loadFromXML_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_loadFromXML_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_loadFromXML_App/?at=master

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial: System class in java [How to modify the system property]


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=ZqkxXiSWxwI&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial: System class in java [How to modify the system property] 
    myProperties.txt
    name=Peter
    email=peter@yahoo.com
    
    
    PropertiesDemo.java
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class PropertiesDemo
    {
    
        public static void main(String[] args) throws IOException
        {
            /*
             * Set up new properties object from file
             * "myProperties.txt"
             */
            FileInputStream fis = new FileInputStream("myProperties.txt");
            /*
             * This statement initializes the new properties
             * object, p, with the current set of system
             * properties
             */
            Properties p = new Properties(System.getProperties());
            p.load(fis);
    
            /*
             * To modify the existing set of system properties,
             * use System.setProperties. This method takes a
             * Properties object that has been initialized to
             * contain the properties to be set. This method
             * replaces the entire set of system properties with
             * the new set represented by the Properties object.         * 
             */
            System.setProperties(p);
            // display new properties
            System.getProperties().list(System.out);
    
        }
    }
    
    Output
    -- listing properties --
    java.runtime.name=Java(TM) SE Runtime Environment
    sun.boot.library.path=C:\Java\jre1.8.0_111\bin
    java.vm.version=25.111-b14
    java.vm.vendor=Oracle Corporation
    java.vendor.url=http://java.oracle.com/
    path.separator=;
    java.vm.name=Java HotSpot(TM) 64-Bit Server VM
    file.encoding.pkg=sun.io
    user.script=
    user.country=US
    sun.java.launcher=SUN_STANDARD
    sun.os.patch.level=Service Pack 1
    java.vm.specification.name=Java Virtual Machine Specification
    user.dir=D:\eclipse\workspace\PropertiesDemo
    java.runtime.version=1.8.0_111-b14
    java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
    java.endorsed.dirs=C:\Java\jre1.8.0_111\lib\endorsed
    os.arch=amd64
    name=Peter
    java.io.tmpdir=C:\Users\Ramesh\AppData\Local\Temp\
    line.separator=
    
    java.vm.specification.vendor=Oracle Corporation
    user.variant=
    os.name=Windows 7
    sun.jnu.encoding=Cp1252
    java.library.path=C:\Java\jre1.8.0_111\bin;C:\Windows\S...
    email=peter@yahoo.com
    java.specification.name=Java Platform API Specification
    java.class.version=52.0
    sun.management.compiler=HotSpot 64-Bit Tiered Compilers
    os.version=6.1
    user.home=C:\Users\Ramesh
    user.timezone=
    java.awt.printerjob=sun.awt.windows.WPrinterJob
    file.encoding=Cp1252
    java.specification.version=1.8
    user.name=Ramesh
    java.class.path=D:\eclipse\workspace\PropertiesDemo\bin
    java.vm.specification.version=1.8
    sun.arch.data.model=64
    java.home=C:\Java\jre1.8.0_111
    sun.java.command=PropertiesDemo
    java.specification.vendor=Oracle Corporation
    user.language=en
    awt.toolkit=sun.awt.windows.WToolkit
    java.vm.info=mixed mode
    java.version=1.8.0_111
    java.ext.dirs=C:\Java\jre1.8.0_111\lib\ext;C:\Windo...
    sun.boot.class.path=C:\Java\jre1.8.0_111\lib\resources.ja...
    java.vendor=Oracle Corporation
    file.separator=\
    java.vendor.url.bug=http://bugreport.sun.com/bugreport/
    sun.cpu.endian=little
    sun.io.unicode.encoding=UnicodeLittle
    sun.desktop=windows
    sun.cpu.isalist=amd64
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_system_modify_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_system_modify_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_system_modify_App/?at=master

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial