ご清聴ありがとうございました。
ログインフォームと登録フォームを使用するプログラムを作成しました。ユーザーがメールアドレスを登録すると、パスワードが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
が次のように保存されているファイルがあるとします。
こんにちは世界
テストテスト
DumbUser 12345
ユーザーsjklfashm-0998()(&
そして、すべての行の最初の単語をusername
として使用し、2番目の単語をpassword
/hash
として使用します。次に、アイデアは次のとおりです。
username
と比較し、2番目の部分を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 {
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));