如何使用 FileReader 逐行读取
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();
}
您可以使用以下代码读取文件..
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));
假设我们有一个包含 username
和 hash
的文件存储如下:
Hello World
Test Test
DumbUser 12345
User sjklfashm-0998()(&
并且我们希望将每行中的第一个单词用作 username
,将第二个单词用作 password
/hash
。那么思路是:
- 读一行
- 在“”处将线分成几部分
- 将第一部分与
username
进行比较,将第二部分与 pass
进行比较
- 如果匹配 return 为真,否则重新开始
导致此代码:
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
标记了我的代码与您的代码不同的部分
感谢您的关注。
我创建了一个使用登录表单和注册表单的程序。
一旦用户注册了他们的电子邮件,他们的密码将被保存到 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();
}
您可以使用以下代码读取文件..
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));
假设我们有一个包含 username
和 hash
的文件存储如下:
Hello World
Test Test
DumbUser 12345
User sjklfashm-0998()(&
并且我们希望将每行中的第一个单词用作 username
,将第二个单词用作 password
/hash
。那么思路是:
- 读一行
- 在“”处将线分成几部分
- 将第一部分与
username
进行比较,将第二部分与pass
进行比较
- 如果匹配 return 为真,否则重新开始
导致此代码:
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