In: Computer Science
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
If you have any doubts, please give me comment...
import java.io.*;
import java.util.Scanner;
public class Excercise{
public static void main(String[] args) throws FileNotFoundException{
Scanner userScnr = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = userScnr.nextLine();
File file = new File("output.txt");
System.out.println("Writing name into a file...");
PrintWriter pw = new PrintWriter(file);
pw.println(name);
pw.close();
System.out.println("Successfully your name saved into output.txt");
System.out.println("\nReading your name from output.txt...");
Scanner fileScnr = new Scanner(file);
name = fileScnr.nextLine();
System.out.println("Your name is from file is: "+name);
fileScnr.close();
}
}