0

I am working on a project where users can create a username and password, and then log-in to the system. Whenever a user creates a new username and password, they are saved into a text file "logins.txt" and each entry is separated by two empty lines, here is an example of the .txt file layout:

Username:admin
Password:password


Username:caro
Password:test

Our team is able to add new users and their passwords to the text file with no problem, but I am running into some issues with my next step. I need to figure out how to write a method that is able to find a matching username and password and delete it from the text file, prompted by the user.

void addData(String usr, String pswd){
        try {
            RandomAccessFile raf = new RandomAccessFile(f+"\\logins.txt", "rw");
            for(int i=0;i<ln;i++){
                raf.readLine();
             }
            //if condition added after video to have no lines on first entry
            if(ln>0){
            raf.writeBytes("\r\n");
            raf.writeBytes("\r\n");
            }
            raf.writeBytes("Username:"+usr+ "\r\n");
            raf.writeBytes("Password:"+pswd+ "\r\n");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

This is a snippet of the code we are using to add the new login information to the .txt file. So far for the delete method, all I have is: void deleteData(String usr,String pswd){}. I am just not sure how to go about deleting a username/password from the .txt file, any suggestions?

  • 3
    [This question](https://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it) about finding and removing lines from files probably contains an answer you can use. – Octa Apr 03 '23 at 20:02

0 Answers0