[英]How to read line by line by using FileReader
感谢您的关注。
我创建了一个程序,我正在使用登录表单和注册表单。 一旦用户注册了他们的电子邮件,他们的密码就会被保存到submit.txt
中。 然后他们将返回登录表单并输入保存在submit.txt
中的电子邮件和密码。
在我的代码中,我将写入文件用于注册表单,将读取文件用于登录表单。 但是,它不起作用。 我知道我在用于读取文件的代码中存在问题。 你能帮我意识到这一点吗?
非常感谢您的帮助。
if (checkPassword(usern, hash)) {
System.out.println("Logged in!");
ChooseWindow ptb = new ChooseWindow();
ptb.setVisible(true);
LoginWindow.this.dispose();
} else {
System.out.println("Wrong password");
}
public boolean checkPassword(String username, String pass) {
try {
FileReader inFile = new FileReader("/users/Ender/Desktop/GetUser/submit.txt");
BufferedReader inStream = new BufferedReader(inFile);
String inString;
while ((inString = inStream.readLine()) != null) {}
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
这是我从文件中读取的代码:
String line;
try {
BufferedReader bufferreader = new BufferedReader(new FileReader("C:\\Users\\ahmad\\Desktop\\aaa.TXT"));
while ((line = bufferreader.readLine()) != null) {
/**
Your implementation
**/
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
假设我们有一个username
和hash
存储如下的文件:
你好世界
测试一下
哑用户 12345
用户 sjklfashm-0998() ( &
我们想使用每行中的第一个单词作为username
,第二个作为password
/ hash
。 那么想法是:
username
进行比较,将第二部分与pass
这导致此代码:
public boolean checkPassword(String username, String pass) {
// if there is no username or no password you can not log in
if(username == null || pass == null){ // diff
return false; // diff
} // diff
try {
FileReader inFile = new FileReader(PASSWORD_FILE);
BufferedReader inStream = new BufferedReader(inFile);
String inString;
while ((inString = inStream.readLine()) != null) {
// we split every line into two parts separated by a space " "
String usernameHash[] = inString.split(" "); // diff
// if there are more or less than two parts this line is corrupted and useless
if(usernameHash.length == 2 // diff
&& username.equals(usernameHash[0]) // diff
&& pass.equals(usernameHash[1])){ // diff
// if username matches the first part and hash the second everything is goo
return true; // diff
} // diff
}
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
我用// diff
标记了我的代码与您的代码不同的部分
try {
FileReader inFile = new FileReader("/users/Ender/Desktop/GetUser/submit.txt");
BufferedReader inStream = new BufferedReader(inFile);
List<String> lines = inStream.Lines().toList();
String username=lines[0];
String password=lines[1];
}
您可以使用以下代码读取文件..
try {
BufferedReader bufferreader = new BufferedReader(new FileReader("./users/Ender/Desktop/GetUser/submit.txt"));
String line;
while ((line = bufferreader.readLine()) != null) {
// line variable contains the readed line
// You can append it to another String to get the whole text or anything you like
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
如果要编写文件,请使用以下代码..
BufferedWriter writer = new BufferedWriter(new FileWriter("./users/Ender/Desktop/GetUser/submit.txt"));
writer.write(your_text);
writer.close();
如果要附加文本,请使用以下代码创建BufferedWriter
的实例
BufferedWriter writer = new BufferedWriter(new FileWriter("/users/Ender/Desktop/GetUser/submit.txt", true));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.