将用户凭据也写入文件

writing user credentials too a file

在 eclipse 中,我正在构建一个非常基本的服务器客户端应用程序。它接受用户输入并将其发送到位于项目目录中的名为 UsersData.txt 的文本文件,以便稍后用于身份验证,我将用户输入放在数组列表中并尝试将其传递到文件中,但确实如此不要太写文件,我也不太确定为什么。 这是应用程序客户端部分的代码 https://gist.github.com/swordslinger/9abbff33bf76d77e4bc4e2779d1ad530/edit

package ie.gmit.sw;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

public class Server extends Thread {
    
    //variables for server
    private Socket conn;
    private int id;
    ObjectOutputStream out;
    ObjectInputStream in;
    
    //variables for taking and sending info too console
    String option;
    String response;
    String message;
    
    //variables for users
    String storedUser="default";
    String storedPass="default";
    String regid = "default";
    String username;
    String password;
    
    //Arraylists for Users and Fleet
    ArrayList<Users> users = new ArrayList<Users>();
    ArrayList<Fleet> fleet = new ArrayList<Fleet>();
    
    //variables for fleet
     String storedName;
     String storedAge;
     String storedMachineId;
     String storedClubId;
     String storedVendor;
     String storedValuation;
     String storedLastSerKm;
     String storedLastSerDate;
     String storedNextSerKm;
     String storedCurKm;
     
     //variable for checking if user is valid
     boolean userExists = false;
    
    public Server(Socket c, int i, ArrayList<Users> user)
    {
        conn = c;
        id = i;
    }
    
    public void run()
    {
        try 
        {
            out = new ObjectOutputStream(conn.getOutputStream());
            in = new ObjectInputStream(conn.getInputStream());
        
            
            //Server Conversation
            
            do
                {
                message = (String)in.readObject();
                
            
            out.writeObject("Press 1 for Login or 2 for Register");
            out.flush();
            
            option = (String)in.readObject();
            
            if(option.equalsIgnoreCase("1"))
            {
                //read from user file too check credentials
                readUser();
                //Login
                out.writeObject("Please enter the userrname and password");
                out.flush();
                
                storedUser = (String)in.readObject();
                
                storedPass = (String)in.readObject();
                
                for (int i = 0; i < users.size(); i++) {
                    // if the credentials match, found = true
                    if (storedUser.equalsIgnoreCase(users.get(i).username)
                        && storedPass.equalsIgnoreCase(users.get(i).password)) {
                        userExists = true;
                        break;
                    }
                    // else it is still false
                    else {
                        userExists = false;
                    }
                }
            }
        
            else if(option.equalsIgnoreCase("2"))
            {
                //Writes too file
                FileWriter fr = new FileWriter("UsersData.txt", true);
                BufferedWriter br = new BufferedWriter (fr);
                //Register
                out.writeObject("Please enter the username, password and ID");
                out.flush();
                
                storedUser = (String)in.readObject();
                storedPass = (String)in.readObject();
                regid = (String)in.readObject();
                
                //store users in array list
                Users newUsers = new Users(storedUser, storedPass);
                
                //output new user too file
                br.write("\n" + storedUser + " " + storedPass);
                users.add(newUsers);
                
                response ="OK";
                out.writeObject("OK");
                
                out.flush();
                //closeing buffer reader and file writer 
                br.close();
                fr.close();
                
            }
            
                }while(option.equalsIgnoreCase("2")|| response.equalsIgnoreCase("FAIL"));
        
        //Logged in menu
        }
        
        catch(Exception e)
        {
            
        }
    }

    //read users from file
    private void readUser() throws FileNotFoundException {
        Scanner s =  new Scanner(new File("UsersData.txt"));
        
        while (s.hasNext()) {
            users.add(new Users(s.next(), s.next()));
        }
        
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ServerSocket listener;
        Socket connection;
        int id=0;
        ArrayList<Users> users = null;
        
        try
        {
            listener = new ServerSocket(25000,10);
            
            while(true)
            {
                
                System.out.println("Listening for a connection");
                connection = listener.accept();
                System.out.println("Received Connection from "+connection.getInetAddress());
                
                Server th = new Server(connection,id, users);
                id++;
                th.start();
            }
        
        }
        catch(Exception e)
        {
            
        }
        
    }

}

“我不太清楚为什么”

你有一个空的catch块,也就是说,当错误发生时,你忽略它。这就是您不确定问题所在的原因。