For testing purpose we need some data from user at run time.It can be provided from external file.There is a way a to do that using properties file.
Basically properties file is holding data in key value pair. Properties file may be in .xml format or in .properties format.
Simple Properties File Looks Like below
User.Name="TestUser"
User.Password="TestPassword"
Here User.Name is as Key And "TestUser" is as value.
Xml Properties file looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="User.Password">"TestPassword"</entry>
<entry key="User.Name">"TestUser"</entry>
</properties>
Here <entry key="User.Password"> key is "User.Password" and Value is "TestPassword"
Reading a properties file we need to initialize first
Properties prop=new Properties();
Then we need to load properties file according to the properties file type if it is normal properties then use this :
prop.load(new FileInputStream("./propertiesFile.properties"));
or if properties file is xml then use
properties.loadFromXML(new FileInputStream("./propertiesFile"));
Now we get the value from properties file using this code.
System.out.println(properties.get("User.Name"));
System.out.println(properties.get("User.Password"));
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package propertiesfilehandling;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesFileHandling {
public static void main(String[] args) throws IOException {
// Reading XML Properties File
Properties properties=new Properties();
properties.loadFromXML(new FileInputStream("./propertiesFile"));
System.out.println(properties.get("User.Name"));
System.out.println(properties.get("User.Password"));
// Reading Properties File
Properties prop=new Properties();
prop.load(new FileInputStream("./propertiesFile.properties"));
System.out.println(prop.get("User.Name"));
System.out.println(prop.get("User.Password"));
}
}
Basically properties file is holding data in key value pair. Properties file may be in .xml format or in .properties format.
Simple Properties File Looks Like below
User.Name="TestUser"
User.Password="TestPassword"
Here User.Name is as Key And "TestUser" is as value.
Xml Properties file looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="User.Password">"TestPassword"</entry>
<entry key="User.Name">"TestUser"</entry>
</properties>
Here <entry key="User.Password"> key is "User.Password" and Value is "TestPassword"
Reading a properties file we need to initialize first
Properties prop=new Properties();
Then we need to load properties file according to the properties file type if it is normal properties then use this :
prop.load(new FileInputStream("./propertiesFile.properties"));
or if properties file is xml then use
properties.loadFromXML(new FileInputStream("./propertiesFile"));
Now we get the value from properties file using this code.
System.out.println(properties.get("User.Name"));
System.out.println(properties.get("User.Password"));
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package propertiesfilehandling;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesFileHandling {
public static void main(String[] args) throws IOException {
// Reading XML Properties File
Properties properties=new Properties();
properties.loadFromXML(new FileInputStream("./propertiesFile"));
System.out.println(properties.get("User.Name"));
System.out.println(properties.get("User.Password"));
// Reading Properties File
Properties prop=new Properties();
prop.load(new FileInputStream("./propertiesFile.properties"));
System.out.println(prop.get("User.Name"));
System.out.println(prop.get("User.Password"));
}
}