2014-10-01 39 views
0

我是Selenium的初學者,我創建了一個叫做ReadPropertyFile的閱讀類名。在這堂課裏,我已經寫了一個讀取config.properties文件的代碼。現在我有一個主要課程,我想從中調用ReadPropertyFile課程以及他們的活動,我可以從中閱讀config.properties文件。下面如何從其他類讀取conf.properties文件?

請幫助我如何做到這一點是代碼:

ReadPropertyFile.java:

public class ReadPropertyFile { 

    public static void main(String[] arg)throws IOException{ 

     // Read configuration properties file 

     String projdir = System.getProperty("user.dir"); 
     String propfilepath = projdir+"\\configuration\\"+"conf.properties"; 
     Properties p = new Properties(); 
     p.load(new FileInputStream(propfilepath)); 

     String url = p.getProperty("URL"); 
     System.out.println(url); // It is returning me a value corresponding to key "test" 
     String driverFilePath = p.getProperty("DRIVER_FILE_PATH"); 
     System.out.println(driverFilePath); 
     String testSuite = p.getProperty("TEST_SUITE"); 
     System.out.println(testSuite); 
    } 
} 

MainClass.java

public class MainClass { 

    static Properties properties= new Properties(); 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     ReadPropertyFile readConfigFile= new ReadPropertyFile(); 
    } 
} 

回答

0

把這個代碼的方法是否可以正常工作

public class ReadPropertyFile { 

public Properties loadPropertiess() throws IOException{ 

// Read configuration properties file 

    String projdir = System.getProperty("user.dir"); 
    String propfilepath = projdir+"\\configuration\\"+"conf.properties"; 
    Properties p = new Properties(); 
p.load(new FileInputStream(propfilepath)); 

    String url = p.getProperty("URL"); 
    System.out.println(url); // It is returning me a value corresponding to key "test" 
    String driverFilePath = p.getProperty("DRIVER_FILE_PATH"); 
    System.out.println(driverFilePath); 
    String testSuite = p.getProperty("TEST_SUITE"); 
    System.out.println(testSuite); 
    return p; 
} } 

然後調用此方法對你的主類

public class MainClass { 

    static Properties properties= null; 

    public static void main(String[] args) throws Exception{ 
// TODO Auto-generated method stub 

    ReadPropertyFile readConfigFile= new ReadPropertyFile(); 

    properties = readConfigFile.loadPropertiess(); 
    } 
} 
0

把你的配置讀取器中的一類了Methode。

而且你只能在時間只使用'main'一次。

public class ReadPropertyFile{ 

      public void readFile(){ 

       // read config