java 从文本文件中读取用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14904344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading User Name and Password From a Text File
提问by DarthOpto
I have a text file with the following format:
UserName
Password
On two separate lines.
I am trying to enter the user name into the user name field (obviously) and the same with the password only into the password field.
My test used to have a String
element defined for each and all was well and good as I would simply call the String element. The problem is that I cannot check in my code, because it has my personal active directory login information. So I am trying to read it from a text file. I have the following code to do this:
我有一个具有以下格式的文本文件:
UserName
Password
在两个单独的行上。我试图在用户名字段中输入用户名(显然),并且只在密码字段中输入密码。我的测试曾经String
为每个元素定义了一个元素,一切都很好,因为我会简单地调用 String 元素。问题是我无法签入我的代码,因为它有我的个人活动目录登录信息。所以我试图从文本文件中读取它。我有以下代码来做到这一点:
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());
}
The problem that I am running into is that it enters the user name and password and then runs through it again only putting in the password into the User Name field. I know that this is probably something simple that I am over looking. Please help.
我遇到的问题是它输入用户名和密码,然后再次运行它,只将密码输入用户名字段。我知道这可能是我在看的一些简单的事情。请帮忙。
回答by Hemant
Did you tried using java.util.Properties class for reading the keys from text file? It is designed for these purposes and can be used to both read/write properties
您是否尝试使用 java.util.Properties 类从文本文件中读取密钥?它专为这些目的而设计,可用于读/写属性
Example code
示例代码
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();
}
回答by Amit
Replace with below code:
替换为以下代码:
try
{
FileInputStream fstream = new FileInputStream("c:/Test/userInfo.txt");
// Use DataInputStream to read binary NOT text.
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
int count = 0;
count++;
while((strLine = br.readLine())!= 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());
}
}
However I did not understand why you are looping. Do you have just one username/password in file...or lots of usernames/passwords?
但是我不明白你为什么要循环。您的文件中是否只有一个用户名/密码……还是很多用户名/密码?