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

时间:2015-12-02 23:54:15

标签: java file-io text-files java.util.scanner printwriter

除非他们知道用户名和密码,否则我写了一个程序让用户不在场。它只是我为了好玩而写的一个程序,并没有真正有用的目的。但是,在测试代码时,我注意到在尝试从最近写入的文件中读取时出现问题。无论出于何种原因,我为username.txt文件创建的扫描程序对象userOut都没有检测到它们是文件中的任何内容(即.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();
    }
}
}

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

1 个答案:

答案 0 :(得分:0)

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

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

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