Java Parsing properties File

Source: Internet
Author: User

In the automated testing process, there will often be some common properties to configure, so that later to use the script, we can choose XML, Excel or JSON format to store the data, but in fact, Java itself provides the properties class to handle the properties file, Although the name is the properties, actually open it to find is a Notepad file, so it looks more intuitive, the following is the implementation code to parse properties file.

The properties file is stored in this way, then save it as xxx.properties.

Gsbamusername1=automation_bam_dg1
Gsbamusername1_firstname=daguang_bam
Gsbamusername1_lastname=zhu

Code:

 PackageUtilities;ImportJava.io.FileInputStream;ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.util.Enumeration;Importjava.util.Properties;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;Importjava.util.Properties; @SuppressWarnings ("Unused") Public classpropertiesoperation {/*** Script Name: <b>PropertiesOperation</b> * Generated: <b>may, 2:03:25 PM&L t;/b> * description:functional Test Script * Original host:winnt Version 5.1 Build 2600 (S)*/     Public StaticString filename1= "Supportfiles/propertyfiles/sourcing.properties";  Public StaticString filename2= "Supportfiles/propertyfiles/testcasemapping.properties";  Public StaticString Filename3 = "Supportfiles/propertyfiles/rfxnumber.properties";  Public StaticString filename4 = "Supportfiles/propertyfiles/rfxnumber.properties";  Public StaticProperties Properties;  Public StaticFileInputStream Inputfile;  Public StaticFileOutputStream OutputFile; Get the Main method in the properties file, according to its key to get Public Staticstring Getsourcingvaluebykey (String key) {String value=""; Try{fileinputstream Inputfile=NewFileInputStream (filename1); Properties Properties=NewProperties ();            Properties.load (Inputfile);                        Inputfile.close (); Value=Properties.getproperty (key); if(Value = =NULL|| Value.equals ("") {System.out.println ("The value for key:" + key + "doesn ' t exist."); System.out.println ("Please check the content of the properties file."); }            }Catch(Exception e) {e.printstacktrace (); }            returnvalue; }            //Added by Justin 06/13//Store The new Key/value pair to the property file and save the file     Public Static voidSetrfxnumberbykey (string key, String value) {String Description= "Property file for Sourcing Automation"; //filename4 = Getrfxnumber ();Properties prop=NewProperties (); Try{FileInputStream fis=NewFileInputStream (FILENAME4);            Prop.load (FIS);            Fis.close ();            Prop.setproperty (key, value); FileOutputStream Fos=NewFileOutputStream (FILENAME4);            Prop.store (FOS, description);        Fos.close (); }Catch(Exception e) {e.printstacktrace (); }    }        //Read the Rfxnumber property file to get the RFx or Item number saved during execution     Public Staticstring Getrfxnumbervaluebykey (String key) {String value=""; //filename4 = Getrfxnumber ();        Try{fileinputstream Inputfile=NewFileInputStream (FILENAME4); Properties Properties=NewProperties ();            Properties.load (Inputfile);                        Inputfile.close (); Value=Properties.getproperty (key); if(Value = =NULL|| Value.equals ("") {System.out.println ("The value for key:" + key + "doesn ' t exist."); System.out.println ("Please check the content of the properties file."); }            }Catch(Exception e) {e.printstacktrace (); }            returnvalue; }    //Read the Testcasemapping property file to get the Spreadsheet Excel filename     Public Staticstring Gettestcasemappingbykey (String key) {String value=""; Try{FileInputStream in=NewFileInputStream (filename2); Properties Settings=NewProperties ();            Settings.load (in); Value=Settings.getproperty (key); if(value==NULL|| Value.equals ("") {System.out.println ("The value for key:" + key + "doesn ' t exist."); System.out.println ("Please check the content of the properties file."); }            }Catch(Exception e) {e.getmessage (); }            returnvalue; }//Public static String Getrfxnumber () {//filename4= Getsourcingvaluebykey ("Gsautodatapath") + "rfxnumber.properties";//        //System.out.println ("filename4:" + filename4);//return filename4;//    }             Public Static voidMain (string[] args)throwsIOException {//Getrfxnumber ();    }}

Java Parsing properties File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.