Java 如何从其他类读取conf.properties文件?
我是Selenium的初学者,我为阅读创建了一个名为Java 如何从其他类读取conf.properties文件?,java,Java,我是Selenium的初学者,我为阅读创建了一个名为ReadPropertyFile的类名。在这个类中,我编写了一个用于读取config.properties文件的代码。现在我有了一个主类,我想从中调用ReadPropertyFile类以及它们的活动,我可以从中读取config.properties文件 请帮助我如何执行此操作,以下是代码: ReadPropertyFile.java: public class ReadPropertyFile { public static void
ReadPropertyFile
的类名。在这个类中,我编写了一个用于读取config.properties
文件的代码。现在我有了一个主类,我想从中调用ReadPropertyFile
类以及它们的活动,我可以从中读取config.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();
}
}
将配置读取器放入类方法中 而且一次只能使用“main”一次
public class ReadPropertyFile{
public void readFile(){
// read config
如果此代码正在工作,请将其放入方法中
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();
}
}