在文件中搜索单词时出错

时间:2016-05-03 01:59:55

标签: java file filewriter printwriter

在随机生成密码的文件中,我的目标是要求输入密码,检查'codes.txt'文件以查看它是否存在,说'登录完成'5秒钟,删除密码,然后关闭文件。当我到达while循环时,我没有按照我需要的方式工作。它在各种情况下都有各种不同的结果,我都无法理解。我甚至没有想出如何在打印'LOGIN COMPLETE'5秒后删除控制台上的东西。如果有人现在可以帮助我,我会非常感激。我的代码位于下方。

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的初学者,所以为了让我理解代码,请使用简单的初学者代码。

1 个答案:

答案 0 :(得分:0)

最后我决定在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();
    }
}