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

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

我写了一个程序来阻止用户进入,除非他们知道用户名和密码。这只是我为了好玩而写的一个程序,它并没有真正的目的。然而,在测试代码时,我注意到了一个问题,当我试图读取最近写入的文件时。无论出于何种原因,我为username.txt文件userOut创建的scanner对象都不会检测到文件中的任何内容(即..hasNextLine()返回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();
    }
}
}

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

您需要重新初始化扫描仪对象,以打开更新文件内容的输入流

System.out.print("Please enter password: ");
String password = in.nextLine().trim();
在上述两行之后,您需要以下内容:

userIn = new Scanner(usernameFile, "UTF-8");
passIn = new Scanner(passwordFile, "UTF-8");
无法读取任何内容的原因是,当文件中没有任何内容时,使用
userIn
passIn
打开与文件用户名和密码的输入流连接。添加以上两行将打开更新文件的新输入流连接,因此,您将能够读取内容

您似乎对以下情况有点不满意:

//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:");
        }

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

您在第一个if条件中有两次userIn.hasnetline()而不是passIn.hasnetline()。是的,我删除了大部分注释,但遗漏了那部分。哎呀。作为旁注,发表评论的人指出了我程序中的一个问题,但通过将passIn而不是userIn进行修复,使password.txt文件报告为空。问题是否出在.hasNextLine()文件上?(我认为如果你更改文件,你根本不必像那样重新打开输入流。)@LukeKrauss你有一个if条件,检查(!userIn.hasnetline()| |!passIn.hasnetline())是否为空,目的是请求user并传递,在该条件内你还有两个if条件。这两个条件都使用userIn.hasnetline(),但是,只有第一个条件应该使用userIn.hasnetline(),而另一个写入password.txt的条件应该使用passIn.hasnetline()。如果您只更新了第一个条件,而保留了另一个条件,那么很明显,当您在第一个条件中写入username.txt时,第二个条件将变为false,因此更新了它们。就重新打开输入流以便能够读取而言,这是一个棘手的问题,您也可以在这个问题中阅读更多内容,您可以使用RandomAccessFile类仅打开文件一次,用于读取和写入。RandomAccessFile可以是正在更新和读取的有用文件