gpt4 book ai didi

java - 从文本文件中读取用户名和密码

转载 作者:行者123 更新时间:2023-11-28 21:05:37 25 4
gpt4 key购买 nike

我有一个格式如下的文本文件:UserNamePassword在两条单独的线上。
我正在尝试将用户名输入用户名字段(显然),并且仅在密码字段中输入密码。
我的测试曾经有一个 String为每个元素定义的元素都很好,因为我会简单地调用 String 元素。问题是我无法 checkin 我的代码,因为它有我的个人 Activity 目录登录信息。所以我试图从文本文件中读取它。我有以下代码来执行此操作:

        try
{
FileInputStream fstream = new FileInputStream(".../Documents/userInfo.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
int count = 0;
strLine = br.readLine();
count++;

while(strLine!= null)
{
//Enter userName
WebElement userName = driver.findElement(By.id("username"));
userName.clear();
userName.sendKeys(strLine);
System.out.println(strLine);

strLine = br.readLine();
count++;
//Enter Password
WebElement password = driver.findElement(By.id("pword"));
password.clear();
password.sendKeys(strLine);
System.out.println(strLine);
}
in.close();
br.close();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}

我遇到的问题是它输入用户名和密码,然后再次运行它,只将密码输入用户名字段。我知道这可能是我正在寻找的简单的东西。请帮忙。

最佳答案

您是否尝试过使用 java.util.Properties 类从文本文件中读取 key ?它专为这些目的而设计,可用于读取/写入属性

示例代码

        Properties prop = new Properties();
prop.load(new FileInputStream(".../Documents/userInfo.txt"));
String userName = prop.getProperty("username");
// Password should always be stored in the char array.
char[] password = null;
if (prop.getProperty("pword") != null) {
password = prop.getProperty("pword").toCharArray();
}

关于java - 从文本文件中读取用户名和密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com