将配置文件中的数据作为
test_data_path = C:\\Sudheer\\Sudheer\\Selinium scripts\\Webdriverscrip\\Automation_Project\\TestData\\Nlpapplication.xlsx
当我运行我的下面脚本时,结果会显示一个Slash缺失
C:\Sudheer\Sudheer\Selinium scripts\Webdriverscrip\Automation_Project\TestData\Nlpapplication.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);
}
}
答案 0 :(得分:1)
你应该覆盖函数加载函数,因为它正在转义\ character。
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);
}
}