问题
Thank you for your attention.
I have created a program which I'm using a Login form and Register form.
Once users register their email and their password will be saved it into submit.txt
. Then they will turn back to login form and enter their email and password which are saved into submit.txt
.
In my code, I'm using write file for Register form, and Read file for Login Form. But, it doesn't work. I know my problem in the code used for Read File. May you help me to realize that?.
Thank you too much for your help.
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;
}
回答1:
here is my code to read from a file :
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();
}
回答2:
Assuming we have a file with username
and hash
stored as follows:
Hello World
Test Test
DumbUser 12345
User sjklfashm-0998()(&
and we want to use the first word in every line as username
and the second as password
/hash
. Then the idea is:
- Read a line
- Split the line into parts at " "
- Compare the first part to
username
and the second topass
- If there is a match return true, else start over
Which results in this code:
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;
}
I marked the parts where my code differs from yours with // diff
回答3:
You can read a file using following code..
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();
}
If you want to write file use following code..
BufferedWriter writer = new BufferedWriter(new FileWriter("./users/Ender/Desktop/GetUser/submit.txt"));
writer.write(your_text);
writer.close();
If you want to append text use following code to create the instance of BufferedWriter
BufferedWriter writer = new BufferedWriter(new FileWriter("/users/Ender/Desktop/GetUser/submit.txt", true));
来源:https://stackoverflow.com/questions/33892453/how-to-read-line-by-line-by-using-filereader