9

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;
 }
Ramesh-X
  • 4,853
  • 6
  • 46
  • 67
Ender phan
  • 185
  • 2
  • 3
  • 10
  • Please provide more details about your problem. Tell us what is wrong with the code (throws exception, does not compile, always returns false, ...). One problem I see is that you read all lines `while ((inString = inStream.readLine()) != null) { }` but don't do anything with them. So `checkPassword` always returns false; – Aracurunir Nov 24 '15 at 11:40
  • My problem is, It could not check the email or password which are entered in submit.txt. I know I'm getting stuck with the code in while{.....}. May you help me ?. – Ender phan Nov 24 '15 at 12:11
  • How are username and hash stored in the file you are reading? If it is like "username hash" in one line you could use `String usernameHash[] = inString.split(" ");` to split your line and then compare username to `usernameHash[0]` and hash to `usernameHash[1]`. If your format is different you might adjust this idea to your file. – Aracurunir Nov 24 '15 at 12:28
  • here is my code as your advices. It debuggs those values in text file, But some of problems here I dont know. – Ender phan Nov 25 '15 at 09:28

4 Answers4

10

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();
    }
Ahmad Alkhatib
  • 1,230
  • 2
  • 14
  • 31
  • thanks for your help. So, my function has 2 variables ( username and pass ), so Do I have to do with it in while loop? ( some how to read and check them ) .. – Ender phan Nov 24 '15 at 12:13
  • This solution is invalid. You are reading the lines using BufferedReader, not FileReader. – kryzystof Mar 12 '21 at 08:35
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 to pass
  • 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

Aracurunir
  • 625
  • 6
  • 10
1
 try {
        FileReader inFile = new  FileReader("/users/Ender/Desktop/GetUser/submit.txt");
        BufferedReader inStream = new BufferedReader(inFile);
        List<String> lines = inStream.Lines().toList();
        String username=lines[0];
        String password=lines[1];    
    }
0

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)); 
Ramesh-X
  • 4,853
  • 6
  • 46
  • 67