Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Writing {
- public static void main(String args []) {
- System.out.print("Enter desired username:");
- // println prints the string and moves the cursor to the next line while print doesn't.
- Scanner scan = new Scanner(System.in);
- String accountName = scan.nextLine();
- FileWriter fWriter = null;
- BufferedWriter writer = null;
- try {
- //try is used as a block to indicate up to where the "catch" function should check.
- fWriter = new FileWriter("C:\\Java trials\\Accounts.txt");
- writer = new BufferedWriter(fWriter);
- writer.write("Username:" + accountName);
- writer.newLine();
- writer.close();
- System.out.println("Your username \"" + accountName + "\" was saved.");
- // .err makes the output RED. When .out would make it the same as everything else.
- //to add quotes to a string or a text line use backslash (\) before the desired quote.
- } catch (Exception e) {
- // catch checks if there's an error up to the try block and if it comes true
- // it prints out "Error!" in the console.
- // (Exception e) calls for the string e which doesn't exist, thus impractical.
- System.out.println("Error!");
- }
- System.out.print("Enter desired password:");
- Scanner scan2 = new Scanner(System.in);
- String accountPass = scan2.nextLine();
- FileWriter fWriter2 = null;
- BufferedWriter writer2 = null;
- try {
- fWriter2 = new FileWriter("C:\\Java trials\\Accounts.txt");
- writer2 = new BufferedWriter(fWriter2);
- writer2.newLine();
- writer2.write("Password:" + accountPass);
- writer2.newLine();
- writer2.close();
- System.out.println("Your password \"" + accountPass + "\" was saved.");
- //Each function has a slightly different string as the values that are being inputed
- // are different and require separate strings.
- } catch (Exception e) {
- System.out.println("Error!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement