Load resources in Java

We can load resources using getResource or getResourceAsStream method of ClassLoader.Here is the example to load the test resource. Note that Environment.properties resource file is placed in the test resources directory. Below code illustrates how to load above resource file in Java.
 
import java.io.InputStream;
import java.util.Properties;

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

        String resourceName = "Environment.properties";
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        Properties props = new Properties();
        try{
            InputStream resourceStream =
                    loader.getResourceAsStream(resourceName);
            props.load(resourceStream);
            System.out.println("webserver.url -> " + props.getProperty("webserver.url"));
        }catch (Exception ex){
            System.out.println("Exception ");
        }
    }
}
Here is the output of above example.

webserver.url -> https://www.softpost.org

Web development and Automation testing

solutions delivered!!