在文件中搜索单词时出错

Error searching for word in file

在随机生成的密码文件中,我的目标是询问密码,检查 'codes.txt' 文件以查看它是否存在,说 'LOGIN COMPLETE' 5 秒,删除密码,然后关闭文件。当我到达 while 循环时,我没有按照我需要的方式工作。它在不同的情况下有各种不同的结果,none我能理解。我什至没有想出如何在打印 5 秒后删除控制台上的内容 'LOGIN COMPLETE'。如果现在有人可以帮助我,我将不胜感激。我的代码位于下方。

package password;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;


public class Password {

    public void creator() throws IOException { 

        FileWriter fw = new FileWriter(new File("codes.txt"));
        PrintWriter out = new PrintWriter(fw);

        char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890".toCharArray();

        Random random = new Random();

        for (int x = 0; x < 51; x++){
            String word = "";
            for (int i = 0; i <= 10; i++) {

                char c = chars[random.nextInt(chars.length)];
                word+=c;

            }
            out.println(word);
        }
        fw.close();
    }

    public void readit() throws FileNotFoundException, InterruptedException {
        File file = new File("codes.txt");

        Scanner input = new Scanner(file);

        //prints each line in the file
        while (input.hasNextLine()) {
            String line = input.nextLine();
            System.out.println(line);
        }
        Thread.sleep(10000);
        input.close();
    }

    public void checkit() throws FileNotFoundException, IOException, InterruptedException {

        File checkFile = new File("codes.txt");
        File tempFile = new File("tempFile.txt");

        Scanner input = new Scanner(System.in);
        Scanner reader = new Scanner(checkFile);
        FileWriter fw = new FileWriter(tempFile);
        PrintWriter out = new PrintWriter(fw);

        System.out.println("What is the password?");
        String word = input.nextLine();

        while(reader.hasNextLine()) {
            String line = input.nextLine();
            if(line.equals(word)){
                System.out.println("LOGIN COMPLETE");
                Thread.sleep(5000);
            } else {
                out.println(line);
            }
        }

        reader.close();
        fw.close();
        checkFile.delete();
        tempFile.renameTo(checkFile);
    }
}

主要文件如下。

package password;

import java.io.FileNotFoundException;
import java.io.IOException;



public class Main {
    public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException {
        Password pass = new Password();

        pass.creator();
        pass.readit();
        pass.checkit();
    }
}

我是 java 的初学者,所以为了让我理解代码,请使用简单的初学者代码。

最后我决定在 Netbeans 中真的没有必要清除控制台屏幕,我将保持原样。我确实想为那些对我想要的东西感到困惑的人以及可能遇到与我相同问题的任何人提供我最终得到的解决方案。

package password;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;


public class Password {

    //Makes 50 random passwords(ten characters using letters and numbers)
    public void creator() throws IOException { 

        FileWriter fw = new FileWriter(new File("codes.txt"));
        PrintWriter out = new PrintWriter(fw);

        char[] chars = "abcdefghijklmnopqrstuvwxyz1234567890".toCharArray();

        Random random = new Random();

        for (int x = 0; x < 51; x++){
            String word = "";
            for (int i = 0; i <= 10; i++) {

                char c = chars[random.nextInt(chars.length)];
                word+=c;

            }
            out.println(word);
        }
        fw.close();
    }

    //prints passwords for 10 seconds
    public void readit() throws FileNotFoundException, InterruptedException {
        File file = new File("codes.txt");

        Scanner input = new Scanner(file);

        //prints each line in the file
        while (input.hasNextLine()) {
            String line = input.nextLine();
            System.out.println(line);
        }
        Thread.sleep(10000);
        input.close();

    }

    //asks for password and if it's correct then states LOGIN COMPLETE and then adds exceptions to a temporary file then readds to main file then closes
    public void checkit() throws FileNotFoundException, IOException, InterruptedException {

        File file = new File("codes.txt");
        FileWriter fw = new FileWriter(new File("code.txt"));
        PrintWriter out = new PrintWriter(fw);


        Scanner reader = new Scanner(file);
        Scanner input = new Scanner(System.in);

        System.out.println("Enter a password");
        String word = input.nextLine();

        //prints each line in the file
        while (reader.hasNextLine()) {

            String line = reader.nextLine();
            if (line.equals(word)) {
                System.out.println("LOGIN COMPLETE");
                Thread.sleep(5000);
            } else {
                out.println(line);
            }
        }
        reader.close();
        fw.close();

        File file2 = new File("code.txt");
        Scanner reader2 = new Scanner(file2);
        FileWriter fw2 = new FileWriter(new File("codes.txt"));
        PrintWriter out2 = new PrintWriter(fw2);

        while (reader2.hasNextLine()) {
            String line = reader2.nextLine();
            out2.println(line);
        }
        file2.delete();
        fw2.close();
        System.exit(0);
    }
}

下面的主要文件:

package password;

import java.io.FileNotFoundException;
import java.io.IOException;



public class Main {
    public static void main(String[] args) throws IOException, FileNotFoundException, InterruptedException {
        Password pass = new Password();

        pass.creator();
        pass.readit();
        pass.checkit();
    }
}