How to Read From Property File in Java

Properties files stores simple parameters as fundamental-value pairs, exterior of compiled code. Primal and Values are separated using '=' sign

Most of the time, properties files are used to store URL, username, countersign, environment, etc..

E.k.:

Cardinal=Value
Desert=hot

Rules about backdrop files

    1. Entries are generally expected to be a unmarried line of the class, one of the post-obit:
            Output : propertyName=propertyValue  propertyName:propertyValue          
    1. White space that appears between the property name and holding value is ignored, then the following are equivalent.
            Output : name=cherchertech name = cherchertech          

White space at the outset of the line is likewise ignored.

    1. Lines that start with the comment characters ! or # are ignored. Blank lines are too ignored.
    2. The property value is generally terminated by the end of the line. White space following the property value is not ignored and is treated as role of the property value.
    3. A property value can bridge several lines if each line is terminated by a backslash ('') character. For case:
            Output : targetCities=         Detroit,         Chicago,         Los Angeles          

This is equivalent to targetCities=Detroit, Chicago, Los Angeles (white space at the outset of lines is ignored).

  1. The characters newline, carriage return, and tab tin can exist inserted with characters, and, respectively.
  2. The backslash character must exist escaped every bit a double backslash. For example: path=c:docsdoc1
  3. UNICODE characters tin exist entered as they are in a Coffee program, using 'u' as a prefix. For instance, u002c.

We can read and write the properties file in selenium using the Backdrop class present in Java; this form provides few methods to read and write the information into the backdrop file.

Insights most backdrop files

  • Yous can store the file anywhere in the local file arrangement, better if you tin place it relative to your project
  • Recommended .properties extension; nevertheless, yous can cull your own extension similar text so on.
  • Properties class extends the Hash tabular array; you tin iterate over the values in a backdrop file like you lot would in a Hashtable.

Follow the below steps to read the property file
i. Create a Java class on Eclipse
read-properties-file-in-java-selenium-webdriver

2. Create a properties file on your local machine with .properties extension.
properties-file-java-selenium-webdriver

3. Create an Object for FileInputStream with a .properties file path; it makes the file into a stream input particular

            // create file input stream object for the properties file FileInputStream fis = new FileInputStream("C:pathconfig.backdrop");          


4. Create a Properties class object to access the belongings file.

            // create object for Backdrop class Properties prop = new Properties();          


5. Load the property file using load() nowadays in the Properties object

            // Load backdrop file prop.load(fis);          


6. Become the values using 'go("key")' method or getProperty("key") method past passing key every bit the patrameter

            prop.getProperty("author"); prop.become("user");          


Complete Programme to Read a value from a properties file with java.

            import coffee.io.FileInputStream; import coffee.io.IOException; import java.util.Properties;  public class ReadProperties { 	public static void main(String[] args) throws IOException { 		// create file input stream object for the properties file 		FileInputStream fis = new FileInputStream("C:pathconfig.properties"); 		// create Properties class object to access properties file 		Properties prop = new Backdrop(); 		// load the properties file 		prop.load(fis); 		// get the property of "url" using getProperty() 		Organisation.out.println(prop.getProperty("url")); 		Organisation.out.println(prop.getProperty("author")); 		// become the property of "url" using go() 		Organisation.out.println(prop.get("user")); 	} }          
            Output : 		https://chercher.tech 		unknown 		whatsoever one in the earth          

HtmlUnitDriver for headless execution in Selenium webdriver

To read files from the classpath, we have to requite the relative path to the file. Nosotros can retrieve the classpath using this.getClass().getResourceAsStream()

All the steps volition remain the same except the path of the file.

            // create file input stream object for the properties file FileInputStream fis = new FileInputStream(this.getClass().getResourceAsStream("/test.properties")); // create Backdrop grade object to access properties file Properties prop = new Backdrop();          

Sometimes properties could be in different format like '.txt' or other extensions, the file could accept any extension withal we can use the same methods what we used in a .backdrop file.

config file
config-p-text-properties-file-java-selenium-webdriver

This program shows how to read Properties files in text format.

            import java.io.FileInputStream; import java.io.IOException; import java.util.Properties;  public class PropertiesTextExtension { 	public static void main(String[] args) throws IOException { 		// create file input stream object for the properties file 		FileInputStream fis = new FileInputStream("C:pathconfig-p.txt"); 		// create Properties class object to access properties file 		Properties prop = new Properties(); 		// load the backdrop file 		prop.load(fis); 		// become the property of "url" using getProperty() 		System.out.println(prop.getProperty("time")); 		// become the property of "url" using get() 		Organisation.out.println(prop.go("hour")); 	} }          

Follow the beneath steps to write property file
1. Create a Java grade on Eclipse
write-properties-file-in-java-selenium

2. Create a properties file on your local machine with .properties extension(config-write.properties).
write-properties-file-java-selenium

three. Create Object for FileOutputStream with a .properties file path; it makes the file into a stream output item

            // create file output stream object for the properties file FileOutputStream fos = new FileOutputStream("C:pathconfig-write.properties");          

4. Create a Properties course object to access the belongings file.

            // create object for Properties class Properties prop = new Properties();          


five. Set the values using getProperty("key", "Value") method by passing fundamental and values as the parameters

            // set the properties prop.setProperty("Selenium", "https://chercher.tech");          


6. Store the values onto file commit it to the local file system using prop.store(fos)

            // prepare the backdrop prop.setProperty("Selenium", "https://chercher.tech");          

seven. Information technology volition store the values forth with the committed time on the first line

Complete Program to Write values to backdrop file with java

            import java.io.FileOutputStream; import coffee.io.IOException; import java.util.Backdrop;  public class WriteProperties { 	public static void main(String[] args) throws IOException { 		// create file output stream object for the properties file 		FileOutputStream fis = new FileOutputStream("C:pathconfig-write.backdrop"); 		// create Properties form object to access properties file 		Backdrop prop = new Properties(); 		// load the properties file 		// set the backdrop 		prop.setProperty("Selenium", "https://chercher.tech"); 		prop.setProperty("Google", "https://google.com"); 		prop.setProperty("Yahoo", "https://yahoo.com"); 		// shop the file into local system 		prop.store(fis, nada); 	} }          

Nearly Author :

I am Pavankumar, Having 8.v years of experience currently working in Video/Live Analytics project.

pewfivemplarity.blogspot.com

Source: https://chercher.tech/java/properties-file-read-write-java-selenium-webdriver

0 Response to "How to Read From Property File in Java"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel