2012-05-01 42 views
-3

我想知道是否有一种方法可以将文本写入用户输入yes或no的文本文件。 我正在做一个密码生成器,我想知道如果我可以问用户是否要保存密码,如果是的话,它会将它保存到文本文件供以后使用。如何获取用户输入和写入文件 - Java

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”,如果是的话,那么你可能会输入“这是我的密码.....”,它会保存到“这是我的密码.....“,并在其下面的行上输入密码。 感谢您的帮助!

+3

Google for * java IO教程*。 –

+3

哎哟,我的眼睛。你能否摆脱所有'新的Random()'并且使*一个'Random'实例在*上调用方法?让人惊讶。 – Joey

回答

1

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