web-dev-qa-db-ja.com

FileReaderを使用して1行ずつ読み取る方法

ご清聴ありがとうございました。

ログインフォームと登録フォームを使用するプログラムを作成しました。ユーザーがメールアドレスを登録すると、パスワードが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;
 }
6
Ender phan

これがファイルから読み取る私のコードです:

 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();
    }
7
Ahmad Alkhatib

usernamehashが次のように保存されているファイルがあるとします。

こんにちは世界
テストテスト
DumbUser 12345
ユーザーsjklfashm-0998()

そして、すべての行の最初の単語をusernameとして使用し、2番目の単語をpassword/hashとして使用します。次に、アイデアは次のとおりです。

  • 行を読む
  • 「」で行を分割します
  • 最初の部分をusernameと比較し、2番目の部分をpassと比較します
  • 一致がtrueを返す場合、それ以外の場合は最初からやり直します。

その結果、このコードが作成されます。

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でマークしました

2
Aracurunir

次のコードを使用してファイルを読み取ることができます。

    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)); 
0
Ramesh-X