In: Computer Science
In Java language. 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.
SOURCE CODE:
aaa.java file
import java.io.*;
import java.util.*;
public class aaa {
public static void main(String[] args) throws
IOException {
Scanner sc=new
Scanner(System.in);
System.out.println("Enter Your
name:");
// reading user name using
scanner
String
s=sc.nextLine();
// opening the file "user.txt" in
write mode using PrintWriter class
PrintWriter writer= new
PrintWriter(new File("user.txt"));
// writing in file
writer.write(s);
// closing the
file
writer.close();
// opening file "user.txt" in
read mode
FileReader f=new
FileReader("user.txt");
int k;
System.out.println("\nThe file
contains the following text:\n");
//printing the contents of the
file
while((k=f.read())!=-1)
{
System.out.print((char)k);
}
f.close();
}
}
CODE SCREENSHOT:
OUTPUT SCREENSHOT:
Output1:-
output-2:-