从控制台上的用户到.txt java的所有输入

时间:2016-08-12 01:57:34

标签: java file java.util.scanner

我希望将控制台中的所有用户输入都复制到.txt文件中。 这是我的代码,谢谢!

import java.util.Scanner;
import java.io.Console;


public class passwordDriver {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    Console cnsl = null;
    String alpha = null;

    boolean checkPass = true;
    // creates a console object
    cnsl = System.console();

    if (cnsl != null) {
        /** // read line from the user input
        alpha = cnsl.readLine("Name: ");

        // prints
        //System.out.println("Name is: " + alpha);
     */
    }


    while (checkPass) {


        try{


            // if console is not null
            if (cnsl != null) {

                //tell the user what the app does
                System.out.println("Type your password and check how strong it is!");


                // read password into the char array
                char[] pwd = cnsl.readPassword("Password: ");



                if (PassCheck.validatePassword(String.valueOf(pwd))) {
                    checkPass = false;
                }
                else {
                    System.out.println("Your password is invalid. ");
                }
            }
        }catch(Exception ex){

            // if any error occurs
            ex.printStackTrace();
        }




    }


}

1 个答案:

答案 0 :(得分:0)

因此,在您的代码中,您正在检查密码,只需输入以下代码:

如果键入的密码错误,那么 - >

BufferedWriter bw = null;

try {
    bw = new BufferedWriter(new FileWriter("passwords.txt", true));
    bw.write(String.valueOf(pwd));
    bw.newLine();
    bw.flush();
} catch (IOException ioe) {
    ioe.printStackTrace();
}