Write a brief program in JAVA that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class FileReadWrite { public static void main(String[] args) throws FileNotFoundException { String name = "Your name"; // change it to your name File file = new File("name.txt"); // write name to file PrintWriter pw = new PrintWriter(file); pw.println(name); pw.close(); // read name from file and print it Scanner fin = new Scanner(file); System.out.println(fin.nextLine()); fin.close(); } }
Get Answers For Free
Most questions answered within 1 hours.