如何获取用户输入和写入文件 - Java

时间:2012-05-01 08:10:16

标签: java file input

我想知道是否有办法将文本写入用户输入是或否的文本文件。 我正在制作密码生成器,我想知道我是否可以询问用户是否要保存密码,如果是,那么它会将其保存到文本文件中供以后使用。

import java.util.Random;

public class pwdgencmd {
    public static void main(String args[]) {

        Random random = new Random();
        char l1 = (char)(random.nextInt(26) + 'A');
        int RI1 = new Random().nextInt(9) + 1;
        char l2 = (char)(random.nextInt(26) + 'A');
        char l5 = (char)(random.nextInt(26) + 'A');
        char l6 = (char)(random.nextInt(26) + 'A');
        char l7 = (char)(random.nextInt(26) + 'a');
        char l9 = (char)(random.nextInt(26) + 'a');
        int RI5 = new Random().nextInt(9) + 1;
        int RI6 = new Random().nextInt(9) + 1;
        int RI7 = new Random().nextInt(9) + 1;
        int RI8 = new Random().nextInt(9) + 1;
        int RI9 = new Random().nextInt(9) + 1;
        int RI10 = new Random().nextInt(9) + 1;
        char l3 = (char)(random.nextInt(26) + 'a');
        int RI3 = new Random().nextInt(9) + 1;
        char l4 = (char)(random.nextInt(26) + 'a');
        int RI4 = new Random().nextInt(9) + 1;
        double version = 1.1;
        String password1 = ("l1 + RI1 + l3 + RI5 + l5 + l7 + RI8 + RI6 + RI7 + l6 + l9 + l3");
        String password2 = ("l4 + l5 + RI6 + l3 + RI8 + RI5 + l6 + l4 + RI5 + RI9 + l7 + l1");
        String password3 = ("l3 + RI8 + RI10 + l4 + RI5 + l7 + l6 + l1 + RI7 + RI1 + l5 + l6 +");
        String Print1 = ("(1)Your password is " + password1 + ".");
        String Print2 = ("(2)Your password is " + password2 + ".");
        String Print3 = ("(3)Your password is " + password3 + ".");




        System.out.println("#Made by Jackson Harris");
        System.out.println("[Version]You are using version: " + version +  " Ranting Reindeer.");

        System.out.println(Print1);
        System.out.println(Print2);
        System.out.println(Print3);
      }
 }

我要找的是询问“您是否希望保存密码?y / n”,如果是,则将密码保存到saved_pa​​sswords.txt。是否也可以保存评论?例如“你想在这个保存中添加评论吗?y / n”如果是,那么也许你会输入“这是我的密码......”它会将文件保存为“这是我的密码” .....“密码在它下面的行上。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

是的,有可能。使用FileOutputStream / FileWriter写入文件。查看Oracle Link