Java 在测试中使用.txt文件中的行作为数据

Java 在测试中使用.txt文件中的行作为数据,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,对于我们团队正在构建的框架,我们需要制作一个文本文件,在运行测试之前进行编辑。该文本文件有两行—web应用程序的URL和包含测试用例的excel文件的位置 现在,为了读取文件,我一直在使用Scanner private static void readFile(String fileName) { try { File file = new File(fileName); Scanner scanner = new Scanner(file); while

对于我们团队正在构建的框架,我们需要制作一个文本文件,在运行测试之前进行编辑。该文本文件有两行—web应用程序的URL和包含测试用例的excel文件的位置

现在,为了读取文件,我一直在使用
Scanner

 private static void readFile(String fileName) {
   try {
     File file = new File(fileName);
     Scanner scanner = new Scanner(file);
     while (scanner.hasNextLine()) {
       System.out.println(scanner.nextLine());
     }
     scanner.close();
   } catch (FileNotFoundException e) {
     e.printStackTrace();
   }
 }
我的代码行话不是最好的,所以试着了解我的要求:

有人能告诉我从文本文件中提取这两行(URL和Excel路径)的正确方向吗?将它们分配给两个不同的变量/对象/函数/您真正想调用的任何对象,然后将它们传递到主测试脚本中,以便测试脚本知道它想要做什么

我用油漆画了一幅画。花了100个小时

如果您想更加详细,还可以使用LineNumberReader。请参见此处:

您可以使用该对象

要写入文件,请执行以下操作:

Properties prop = new Properties();
OutputStream output = null;

try {

    output = new FileOutputStream("yourfile.txt");

    // set the properties value
    prop.setProperty("database", "localhost");
    prop.setProperty("dbuser", "john");
    prop.setProperty("dbpassword", "password");

    // save properties to project root folder
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
全文如下:

Properties prop = new Properties();
InputStream input = null;

try {

    input = new FileInputStream("yourfile.txt");

    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
您将得到一个包含以下行的“yourfile.txt”文件:

dbpassword=password
database=localhost
dbuser=john

我的代码来自:

文件的格式是什么?实际上,只要每个路径都在自己的行上,代码就应该能够很好地读取它们?格式为.txt。它是一个小记事本。它读起来很好,我只是想知道如何让它读出来,而不是把它传给我的脚本@svarog从未使用过属性。我现在就读,让你知道。试着使用Reader对象并通过字符串str1=Reader.readLine()分配。正确,如果你不改变文本文件布局,你会没事的。见下面的答案。
dbpassword=password
database=localhost
dbuser=john