PROGRAMMING

m9/ JAVA
REMEMBERS




Last update:   24-10-2021

Properties

Java properties file is normaly used to store project configuration data or settings. /LearningApp/config.properties
 
db.url = localhost
db.user = catalin
db.pass = password
db.port = 9000
/LearningApp/LearningApp.java
 
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.Properties;

/**
 * Load properties from file and 
 * put the properties in Properties object;
 * use getProperty("name") to get one property
 */
class LearningApp {
    public static void main(String[] args) {

        try {

            InputStream input = new FileInputStream(
                "config.properties"
            );
            Properties props = new Properties();
            props.load(input);

            String user = props.getProperty("db.user");
            int port = Integer.parseInt(
                props.getProperty("db.port")
            );

            System.out.println("user: " + user); // catalin
            System.out.println("port: " + port); // 9000

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
... 22 lines
 

LOOP

Loop through Properties using Enumeration.
 
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.Properties;

class App {
    public static void main(String[] args) {

        try {

            InputStream input = new FileInputStream(
                "src/config.properties"
            );
            Properties props = new Properties();
            props.load(input);

            Enumeration e = props.propertyNames();
            while(e.hasMoreElements()) {
                String key = (String) e.nextElement();
                System.out.println(key + " - " + props.getProperty(key));
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
... 17 lines
 
Use list and System.out for quick parsing.
 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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

        InputStream input = new FileInputStream(
            "src/config.properties"
        );
        Properties props = new Properties();
        props.load(input);
        props.list(System.out);

        /*
            -- listing properties --
            db.user=catalin
            db.pass=password
            db.port=9000
            db.url=localhost
        */
    }
}
... 15 lines
 
Use forEach and lambdas for quick parsing.
 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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

        InputStream input = new FileInputStream(
            "src/config.properties"
        );
        Properties props = new Properties();
        props.load(input);
        props.forEach((k, v) -> {
            System.out.println(k + "=" + v);
        });

        /*
            db.user=catalin
            db.pass=password
            db.port=9000
            db.url=localhost
        */
    }
}
... 16 lines
 

System

Get and output System properties.
 
import java.util.Properties;

class App {

    public static void main(String[] args) {
        
        Properties props = System.getProperties();
        for (String name : props.stringPropertyNames()) {
            System.out.printf(
                "%s=%s\n", name, props.getProperty(name)
            );
        }

        // System.getProperties().list(System.out);

        /*
            ...
            os.name=Linux
            java.vm.specification.version=11
            sun.java.launcher=SUN_STANDARD
            user.country=US
            sun.boot.library.path=/usr/lib/jvm/java-11-openjdk-amd64/lib
            ....
        */
    }
}
... 15 lines
 
 
class App {
    public static void main(String[] args) {
        System.getProperties().list(System.out); 
            // compact code version
    }
}

Questions     github Collections
Streams