How to read line by line using FileReader

Thank you for your attention.

I created a program in which I use the "Login Form" and "Registration" form. Once users register their email address, their password will be saved to submit.txt. Then they will return to the login form and enter your email address and password, which are stored in submit.txt.

In my code, I use a write file for the Register form and a Read file for the login form. But that will not work. I know my problem in the code used for Read File. Can you help me figure this out?

Thank you so 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;
 }
+6
source share
3

:

 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();
    }
+6

, username hash, :

Hello World
-
DumbUser 12345
sjklfashm-0998() (&

username, - password/hash. :

  • ""
  • username, - pass
  • , true, else start over

:

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

, .

    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();

If you want to add text, use the following code to create an instance BufferedWriter

    BufferedWriter writer = new BufferedWriter(new FileWriter("/users/Ender/Desktop/GetUser/submit.txt", true)); 
0
source

Source: https://habr.com/ru/post/1617232/


All Articles