'Trying to confirm username and password is in text file using Java and Netbeans

I am trying to complete the last part of my Java code in Netbeans as I am fairly new. I have a client/server. I created a text file called "account.txt" and I have a username and password inside it. I'm trying to complete my code by verifying the username and password that was entered by the client is in the text file. I am including the Server code file. How do I read the username and password entered and compare them to what’s in the text file to give access to the client. I was able to write to the text file but I want to read the file to compare what the client entered is the same as what’s in the text file. I have a note in my code of where I believe the code should be.

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.stream.Stream;

public class Server {
    ServerSocket serversocket;
    Socket client;
    int bytesRead;
    Connect c = new Connect();
    BufferedReader input;
    PrintWriter output;     

public void start() throws IOException{
    System.out.println("Connection Starting on port:" + c.getPort());
    //make connection to client on port specified
    serversocket = new ServerSocket(c.getPort());

    //accept connection from client
    client = serversocket.accept();

    System.out.println("Waiting for connection from client");

    try {
        logInfo();
    } catch (Exception e) {
        // TODO Auto-generated catch block

    }
}
public void logInfo() throws Exception{
    //open buffered reader for reading data from client
    input = new BufferedReader(new InputStreamReader(client.getInputStream()));

    String username = input.readLine();
    System.out.println("SERVER SIDE" + username);
    String password = input.readLine();
    System.out.println("SERVER SIDE" + password);
    
    // *************update this add read text file********************
    File file = new File("accounts.txt");
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);     

        
    if(username.equals(c.getUsername()) &&password.equals(c.getPassword())){
        output.println("Welcome, " + username);
    }else{
        output.println("Login Failed");
    }
    output.flush();
    output.close();
 }
public static void main(String[] args){
    Server server = new Server();
    try {
        server.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block

    }
}

}



Solution 1:[1]

Well, I finally answered my own question on here. How I got it to work was by creating three files, client, server, and log in. I called the login method from the client, so when the client authenticates the login calls the server to proceed with the file request. if the file exists, it will send it through the port but if it doesn't it will let the client know the file does not exist. Let me know if you want to see my finished code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christina