Friday, October 22, 2021

Read Properties file using Java | Three ways

Three ways to read properties file using Java code.

 

1. Lazy Initialization way: Use when needed, read properties file every time.

<<<<<<<
import java.io.FileInputStream;
import java.util.Objects;
import java.util.Properties;

public final class ReadPropertyFile {
    private ReadPropertyFile() {
    }

    public static String getValue(String key) throws Exception {
        Properties property = new Properties();
        FileInputStream file = new FileInputStream(System.getProperty("user.dir") + "/src/test/resources/conf/config.properties");
        property.load(file);
        String value = property.getProperty(key);
        if (Objects.isNull(value)) {
            throw new Exception("Property name " + key + " is not found. Please check the config.properties");
        }
        return value;
    }
}
>>>>>>>

2. Eager Initialization way - Hashtable: Read properties file at start once and store it in Hashtable.

<<<<<<<
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;

public final class ReadPropertyFile {
    private ReadPropertyFile() {
    }

    private static Properties property = new Properties();

    static {
        try {
            FileInputStream file = new FileInputStream(System.getProperty("user.dir") 
                    + "/src/test/resources/conf/config.properties");
            property.load(file);    //Store in java Hashtable - little slow
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getValue(String key) throws Exception {
        if (Objects.isNull(property.getProperty(key)) || Objects.isNull(key)) {
            throw new Exception("Property name " + key + " is not found. Please check the config.properties");
        }
        return property.getProperty(key);
    }
}
>>>>>>>       

3. Eager Initialization way - HashMap: Read properties file at start once and store it in Hashtable, then convert Hashtable into HashMap for faster operation.

<<<<<<<
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

public final class ReadPropertyFile {
    private ReadPropertyFile() {
    }

    private static Properties property = new Properties();
    private static final Map CONFIG_MAP = new HashMap<>();
    static {
        try {
            FileInputStream file = new FileInputStream(System.getProperty("user.dir")
                    + "/src/test/resources/conf/config.properties");
            property.load(file);    //Store in java Hashtable - little slow

            // Convert Hashtable to HashMap
            // Store all properties in HashMap
            for (Object key : property.keySet()) {
                CONFIG_MAP.put(String.valueOf(key), String.valueOf(property.get(key)));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getValueFromMap(String key) throws Exception {
        if (Objects.isNull(CONFIG_MAP.get(key)) || Objects.isNull(key)) {
            throw new Exception("Property name " + key + " is not found. Please check the config.properties");
        }
        return CONFIG_MAP.get(key);
    }
}
>>>>>>>      

Read CSV from S3

 import csv def count_records(csv_file):     record_count = 0     first_line = None     last_line = None     # Open the CSV file and read it...