一个扫描仪对象未检测到程序中写入的文件中的任何内容

One Scanner object not detecting anything in a file written to within the program

我写了一个程序来阻止用户,除非他们知道用户名和密码。这只是我为了好玩而写的一个程序,并没有真正的目的。但是,在测试代码时,我在尝试读取最近写入的文件时注意到一个问题。无论出于何种原因,我为 username.txt 文件创建的扫描器对象 userOut 未检测到文件中的任何内容(即 .hasNextLine() returns false),但仅 如果它是在程序中写入的。如果我向文件写入一些文本,然后 运行 程序,它就可以正常工作。请注意 password.txt 文件完全可以正常工作。为什么会这样?

这是我的代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Challenge5 {

public static void main(String[] args) {
    try {
        File usernameFile = new File("username.txt");
        File passwordFile = new File("password.txt");

        PrintWriter userOut = new PrintWriter(new FileOutputStream(usernameFile, true));
        PrintWriter passOut =  new PrintWriter(new FileOutputStream(passwordFile, true));

        Scanner in = new Scanner(System.in);

        Scanner userIn = new Scanner(usernameFile, "UTF-8");
        Scanner passIn = new Scanner(passwordFile, "UTF-8");

        System.out.println("Welcome to Luke's secure program.");
        if (!userIn.hasNextLine() || !userIn.hasNextLine()) {
            System.out.println("New users must set up their account: ");
            if (!userIn.hasNextLine()) {
                System.out.print("Enter a username: ");
                String username = in.nextLine().trim();
                userOut.print(username);
                userOut.flush();

            }
            if (!userIn.hasNextLine()) {
                System.out.print("Enter a password: ");
                String password = in.nextLine().trim();
                passOut.print(password);
                passOut.flush();

            }
            System.out.println("Your account has been created. You may now log in:");
        }

        System.out.print("Please enter username: ");
        String username = in.nextLine().trim();

        System.out.print("Please enter password: ");
        String password = in.nextLine().trim();

        String fileUsername = "";           
        while (userIn.hasNextLine()) {
            fileUsername = userIn.nextLine();
        }
        System.out.println(fileUsername);

        String filePassword = "";
        while (passIn.hasNextLine()) {
            filePassword = passIn.nextLine();
        }
        System.out.println(filePassword);

        if (username.equals(fileUsername) && password.equals(filePassword)) {
            System.out.println("Welcome!");


            while (true) {
                System.out.println("What would you like to do? ");
                System.out.println("1. Change username\n2. Change password\n3. Exit");
                System.out.print("> ");

                int choice;
                while (!in.hasNextInt()) {
                    System.out.println("What would you like to do? ");
                    System.out.println("1. Change username\n2. Change password\n3. Exit");
                    System.out.print("> ");
                    in.next();
                }
                choice = in.nextInt();
                in.nextLine();

                if (choice == 1) {
                    userOut = new PrintWriter(new FileOutputStream(usernameFile, false));
                    System.out.print("Enter new username: ");
                    String newUsername = in.nextLine().trim();
                    userOut.print(newUsername);
                    userOut.flush();
                } else if (choice == 2) {
                    passOut = new PrintWriter(new FileOutputStream(passwordFile, false));;
                    System.out.print("Enter new password: ");
                    String newPassword = in.nextLine().trim();
                    passOut.print(newPassword);
                    passOut.flush();
                } else if (choice == 3) {
                    System.out.println("Goodbye!");
                    break;
                } else {
                    System.out.println("Please try again.");
                }
            }
        } else {
            System.out.println("INVALID USERNAME OR PASSWORD, EXITING PROGRAM...");
        }

        userOut.close();
        passOut.close();
        in.close();
        userIn.close();
        passIn.close();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
}

我知道我的代码并不完美,但我认为它至少应该可以工作。任何帮助将不胜感激。

您需要重新初始化您的 Scanner 对象以打开更新文件内容的输入流。

System.out.print("Please enter password: ");
String password = in.nextLine().trim();

在上面两行之后,您需要以下内容:

userIn = new Scanner(usernameFile, "UTF-8");
passIn = new Scanner(passwordFile, "UTF-8");

你无法读取任何内容的原因是因为当文件中没有任何内容时,你使用 userInpassIn 打开到文件用户名和密码的输入流连接。添加以上两行打开新的输入流连接到更新的文件,因此您将能够读取内容。

您的以下情况似乎有点问题:

//if (!userIn.hasNextLine() || !userIn.hasNextLine()) {
  if (!userIn.hasNextLine() || !passIn.hasNextLine()) {
            System.out.println("New users must set up their account: ");
            if (!userIn.hasNextLine()) {
                System.out.print("Enter a username: ");
                String username = in.nextLine().trim();
                userOut.print(username);
                userOut.flush();

            }
            //why again !userIn.hasNextLine(), you should check passIn
            //if (!userIn.hasNextLine()) {
              if (!passIn.hasNextLine()) {
                System.out.print("Enter a password: ");
                String password = in.nextLine().trim();
                passOut.print(password);
                passOut.flush();

            }
            System.out.println("Your account has been created. You may now log in:");
        }

我已经修复了这两个条件并注释掉了你的代码位。