How do I convert Properties into Map?

package org.kodejava.util;

import java.util.Properties;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;

public class PropertiesToMap {
    public static void main(String[] args) {
        // Create a new instance of Properties.
        Properties properties = new Properties();

        // Populate properties with a dummy application information
        properties.setProperty("app.name", "HTML Designer");
        properties.setProperty("app.version", "1.0");
        properties.setProperty("app.vendor", "HTML Designer Inc");

        // Create a new HashMap and pass an instance of Properties. Properties
        // is an implementation of a Map which keys and values stored as in a
        // String.
        Map<Object, Object> map = new HashMap<>(properties);

        // Get the entry set of the Map and print it out.
        Set<Map.Entry<Object, Object>> propertySet = map.entrySet();
        for (Map.Entry<Object, Object> entry : propertySet) {
            System.out.printf("%s = %s%n", entry.getKey(), entry.getValue());
        }
    }
}

The output of the code snippet above:

app.vendor = HTML Designer Inc
app.name = HTML Designer
app.version = 1.0

How do I create a multithreaded program?

Java language which was there when the language was created. This multithreading capabilities can be said like running each program on their on CPU, although the machine only has a single CPU installed.

To create a Thread program in Java we can extend the java.lang.Thread class and override the run() method. But as we know that Java class can only extend from a single class, extending Thread class makes our class cannot be inherited from another class. To solve this problem an interface was introduced, the java.lang.Runnable.

Let see the simplified demo class below. In the program below we’ll have three separated thread executions, the main thread, thread-1 and thread-2.

package org.kodejava.lang;

public class ThreadDemo extends Thread {
    public static void main(String[] args) {
        // Creates an instance of this class.
        ThreadDemo thread1 = new ThreadDemo();

        // Creates a runnable object.
        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 5; i++) {
                    sayHello();
                }
            }
        });

        // Set thread priority, the normal priority of a thread is 5.
        thread1.setPriority(4);
        thread2.setPriority(6);

        // Start the execution of thread1 and thread2
        thread1.start();
        thread2.start();

        for (int i = 0; i < 5; i++) {
            sayHello();
        }
    }

    public void run() {
        for (int i = 0; i < 5; i++) {
            sayHello();
        }
    }

    /**
     * The synchronized modifier ensure that two threads cannot execute the block
     * at the same time.
     */
    private static synchronized void sayHello() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Thread [" + Thread.currentThread().getName() +  "] ==> Hi...");
        }

        try {
            // Causes the currently executing thread to sleep for a random
            // milliseconds
            Thread.sleep((long) (Math.random() * 1000 + 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Causes the currently executing thread object to temporarily pause
        // and allow other threads to execute.
        Thread.yield();
    }
}

How do I get the list of file system root?

The code below demonstrates how to obtain file system root available on your system. In Linux, you will have a single root (/) while on Windows you could get C:\ or D:\ that represent the root drives.

package org.kodejava.io;

import java.io.File;

public class FileSystemRoot {
    public static void main(String[] args) {
        // List the available filesystem roots.
        File[] root = File.listRoots();

        // Iterate the entire filesystem roots.
        for (File file : root) {
            System.out.println("Root: " + file.getAbsolutePath());
        }
    }
}

The result of the code snippet:

Root: C:\
Root: D:\
Root: F:\

How do I check if a directory is not empty?

package org.kodejava.io;

import java.io.File;

public class EmptyDirCheck {
    public static void main(String[] args) {
        File file = new File("D:/Downloads");

        // Check to see if the object represent a directory.
        if (file.isDirectory()) {
            // Get list of file in the directory. When its length is not zero
            // the folder is not empty.
            String[] files = file.list();

            if (files != null && files.length > 0) {
                System.out.println(file.getPath() + " is not empty!");
            }
        }
    }
}

How do I decrypt an object with DES?

On the previous example How do I encrypt an object with DES? we encrypt an object. In this example we will decrypt the stored object.

package org.kodejava.crypto;

import java.io.*;

import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;

public class ObjectDecrypt {
    public static void main(String[] args) throws Exception {
        // Read the previously stored SecretKey.
        SecretKey key = (SecretKey) readFromFile("secretkey.dat");

        // Read the SealedObject
        SealedObject sealedObject = (SealedObject) readFromFile("sealed.dat");

        // Preparing Cipher object from decryption.
        if (sealedObject != null) {
            String algorithmName = sealedObject.getAlgorithm();

            Cipher cipher = Cipher.getInstance(algorithmName);
            cipher.init(Cipher.DECRYPT_MODE, key);

            String text = (String) sealedObject.getObject(cipher);
            System.out.println("Text = " + text);
        }
    }

    // Method for reading object stored in a file.
    private static Object readFromFile(String filename) {
        try (FileInputStream fis = new FileInputStream(filename);
             ObjectInputStream ois = new ObjectInputStream(fis)) {

            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}