I am wondering if there is a way to write text to a text file of the user typed in yes or no.
I am making a password generator and i was wondering if i could ask the user if they want to save the password or not, and if yes then it would save it to a text file for later use.
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);
}
}
What I am looking for is it to ask “Do you wish to save the password(s)? y/n” and if it is yes that it will save the passwords to saved_passwords.txt. Is it also possible to save a comment to it? e.g “Do you want to add a comment to this save? y/n” if yes then perhaps you would type “This is my password for …..” and it would save to the file as “This is my password for …..” with the password on the line below it.
Thanks for any help!
Yes, it is possible. Use FileOutputStream/FileWriter to write to file. Check Oracle Link