2

Java では、文字列を受け取る 2 つの JTextField を使用できるようにしたいと考えています。1 つはユーザー名で、もう 1 つはパスワードです。「ログイン」するには、プログラムはファイル --> accounts.txt を検索し、最初に「usrnm:」を検索します。それが見つかると、「:」の直後の単語を取ります。パスワードも同様ですが、検索します。 「pswrd:」の場合。

これまでに得たものは次のとおりです。

    public void checkCredentials() {
    try {
        BufferedReader br = new BufferedReader(new FileReader(new File("accounts.txt")));
        String text = br.readLine();
        text = text.toLowerCase();
        text.split("usrnm:");
        System.out.println("username: " + userInput);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

どんな助けでも大歓迎です!

4

2 に答える 2

3

ファイルを 2 回検索する代わりに、最初にユーザー名を保存し、次の行にパスワードを保存する方がよいと思います。それらを読みながら、次の属性 (ユーザー名とパスワード (文字列)) を持つユーザー オブジェクトを作成できます。また、システムがアクティブな間、それらをリンクされたリストに保持することもできます。ユーザーアカウントに簡単にアクセスできるようにします。ユーザーがログインする必要がある場合は、リストをスキャンし、userList.get(i).equals(textfield.getText()) を使用して、ユーザーが要求したかどうかを判断します。その後、上記のステートメントが当てはまる場合は、userList.get(i).getPassword().equals(textfield2.getText()) が true であるかどうかを確認し、それに応じてアクセスを許可または拒否します。

以下にいくつかの便利なパーツを提供しています。

    public void readFromFile(String fileName, ListInterface<User> userList) {
        String oneLine, oneLine2;
        User user;
        try {
            /*
             * Create a FileWriter object that handles the low-level details of
             * reading
             */
            FileReader theFile = new FileReader(fileName);

            /*
             * Create a BufferedReader object to wrap around the FileWriter
             * object
             */
            /* This allows the use of high-level methods like readline */
            BufferedReader fileIn = new BufferedReader(theFile);

            /* Read the first line of the file */
            oneLine = fileIn.readLine();
            /*
             * Read the rest of the lines of the file and output them on the
             * screen
             */
            while (oneLine != null) /* A null string indicates the end of file */
            {
                oneLine2 = fileIn.readLine();
                user = new User(oneLine, oneLine2);
                oneLine = fileIn.readLine();
                userList.append(user);
            }

            /* Close the file so that it is no longer accessible to the program */
            fileIn.close();
        }

        /*
         * Handle the exception thrown by the FileReader constructor if file is
         * not found
         */
        catch (FileNotFoundException e) {
            System.out.println("Unable to locate the file: " + fileName);
        }

        /* Handle the exception thrown by the FileReader methods */
        catch (IOException e) {
            System.out.println("There was a problem reading the file: "
                    + fileName);
        }
    } /* End of method readFromFile */


    public void writeToFile(String fileName, ListInterface<User> userList) {
        try {
            /*
             * Create a FileWriter object that handles the low-level details of
             * writing
             */
            FileWriter theFile = new FileWriter(fileName);

            /* Create a PrintWriter object to wrap around the FileWriter object */
            /* This allows the use of high-level methods like println */
            PrintWriter fileOut = new PrintWriter(theFile);

            /* Print some lines to the file using the println method */
            for (int i = 1; i <= userList.size(); i++) {
                fileOut.println(userList.get(i).getUsername());
                fileOut.println(userList.get(i).getPassword());
            }
            /* Close the file so that it is no longer accessible to the program */
            fileOut.close();
        }

        /* Handle the exception thrown by the FileWriter methods */
        catch (IOException e) {
            System.out.println("Problem writing to the file");
        }
    } /* End of method writeToFile */
  • userList は、ジェネリックを使用する動的リンク リストです。(ListInterface<User>)

    ジェネリックを使用したくない場合は、ListInterface userList と言うことができます。

    • User クラスは、同等のものを実装し、以下に示すメソッドを含める必要があります。
    public int compareTo(User user) {
    
    }
    
    public boolean equals(Object user) {
    
    }
    
    • ジェネリックを使用しない場合は、型キャストが必要になる場合があることに注意してください。そうしないと、コンパイル エラーが発生します。
于 2014-01-07T00:08:04.230 に答える