Java Tutorial
IntroductionEnvironment SetupIDEBuild ManagementLanguage SpecificationBasic ProgramVariablesData TypesPackagesModifiersConditionalsLoopsObject OrientedClassesSuperInterfacesEnumStatic importInheritanceAbstractionEncapsulationPolymorphismBoxing & UnboxingConversion Formatting numbers Arrays Command line arguments in java Variable Number of arguments in Java Exception handling in Java String handling in Java StringBuffer and StringBuilder in Java Mathematical Operations in Java Date and Time in Java Regular expressions in Java Input output programming in Java File Handling Nested Classes Collections Generics Serialization Socket programming Multi-Threading Annotations Lambda Expressions Reflections in Java Singleton class in Java Runtime Class in JavaHow to load resource in JavaHow to load properties file in JavaAdvanced
Log4j – Logging framework in JavaInterview Questions in JavaLoad properties file in Java
We can load properties file in Java as shown in below example. Note that Environment.properties file is kept in test resources directory.
import java.io.InputStream;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
String resourceName = "Environment.properties";
Properties props = new Properties();
try{
InputStream resourceStream = null;
resourceStream = ClassLoader.getSystemResourceAsStream(resourceName);
//resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
props.load(resourceStream);
System.out.println("webserver.url -> " + props.getProperty("webserver.url"));
props.list(System.out);
}catch (Exception ex){
System.out.println("Exception " + ex.toString());
}
}
}
Here is the output of above example.
webserver.url -> https://www.softpost.org
— listing properties —
webserver.admin=watson
webserver.url=https://www.softpost.org
Web development and Automation testing
solutions delivered!!