java—将用户凭据写入文件

bweufnob  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(370)

在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)
        {

        }

    }

}
krugob8w

krugob8w1#

“我不太清楚为什么”
有一个空的catch块,也就是说,当发生错误时,忽略它。这就是为什么你不确定问题是什么。

相关问题