Pergunta

I'm trying to make a program that requires the user to log in before they can use it. I created a register button that saves the username and the password in two different text files. When the user presses the Login button, the program should check if the username of the first file matches with the password of the second file. I was wondering how to code the Login button (By the way, don't mind the names, they're in French).

Here is my register button's code:

public class Enregistrement extends GuiLogIn {


public class Enregistrement() {

    registerButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            nom = userText.getText();
            motDePass = passwordText.getText();

            if (!nom.equals("") ){
                if (!motDePass.equals("")) {
                    try {
                        fichierInscriptionNom = new FileWriter("C:\\Users\\bhuglahm\\Desktop\\Utilisateur.txt", true);
                        fichierInscriptionMotDePasse = new FileWriter("C:\\Users\\bhuglahm\\Desktop\\Mot de passes.txt", true);
                        tampon1= new BufferedWriter(fichierInscriptionMotDePasse);
                        tampon = new BufferedWriter(fichierInscriptionNom);
                        tampon.write(nom+"\r\n");
                        tampon1.write(motDePass+"\r\n");

                            JOptionPane.showMessageDialog(null,  "Votre comte est enrigistré avec succées","Enrigistrement", JOptionPane.INFORMATION_MESSAGE);

                    } catch (IOException e1) {

                             e1.printStackTrace();
                    }
                    finally {
                       try{
                           tampon.flush();
                           tampon.close();  
                           fichierInscriptionNom.close();
                           tampon1.flush();
                           tampon1.close(); 
                           fichierInscriptionMotDePasse.close();

                      }
                      catch (IOException ex) {
                          ex.printStackTrace();
                      }
                   }
                }else JOptionPane.showMessageDialog(null,  "Veulliez entrer votre mot de pass ","Enrigistrement", JOptionPane.INFORMATION_MESSAGE);
            }else JOptionPane.showMessageDialog(null,  "Veulliez entrer votre nom d'utilisateur","Enrigistrement", JOptionPane.INFORMATION_MESSAGE);
         }
      });
   }
 }
Foi útil?

Solução

at first i think your way is not so good and it is better to save usernames and passwords in the same file. but with this code you can read the lines of file:

try{

   File file = new File("type your address");

   FileReader reader = new FileReader(file);

   bufferedReader = new BufferedReader(reader);

   String currentLine;

   while((currentLine = bufferedReader.readLine()) != null){

       //in this loop you can access to lines of file by currenLine variable

   }

}catch(Exception IOException){

}finally{

   bufferedReader.close();

}

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top