将配置文件中的数据作为test_data_path = C:\Sudheer\Sudheer\Selinium scripts\Webdriverscrip\Automation_Project\TestData\Nlpapplication.xlsx
当我运行下面的脚本时,结果显示缺少一个斜杠
C:SudheerSudheerSelinium scriptsWebdriverscripAutomation_ProjectTestDataNlpapplication.xlsx
public class Testconfigvalues {
public static void main(String[] args) throws IOException {
FileInputStream fs = null;
fs = new FileInputStream(System.getProperty("user.dir")+"\config.properties");
Properties property=new Properties();
property.load(fs);
String data_test_data_path = property.getProperty("test_data_path");
System.out.println("value is " +data_test_data_path);
}
}
您应该覆盖函数加载函数,因为它正在转义\字符。
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
public class NetbeansProperties extends Properties {
@Override
public synchronized void load(Reader reader) throws IOException {
BufferedReader bfr = new BufferedReader(reader);
ByteArrayOutputStream out = new ByteArrayOutputStream();
String readLine = null;
while ((readLine = bfr.readLine()) != null) {
out.write(readLine.replace("\", "\\").getBytes());
out.write("n".getBytes());
}
InputStream is = new ByteArrayInputStream(out.toByteArray());
super.load(is);
}
@Override
public void load(InputStream is) throws IOException {
load(new InputStreamReader(is));
}
}
类从配置文件读取数据:
import java.io.FileInputStream;
import java.io.IOException;
public class ReadConfig {
public static void main(String[] args) throws IOException {
FileInputStream fs = null;
fs = new FileInputStream(System.getProperty("user.dir") + "\config.properties");
NetbeansProperties property = new NetbeansProperties();
property.load(fs);
String data_test_data_path = property.getProperty("test_data_path");
System.out.println("value is " + data_test_data_path);
}
}