java program;
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.
//ReadWriteFile.java import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Scanner; public class ReadWriteNameFile{ public static void main(String[] args) throws Exception{ String myName = "ronaldo"; FileWriter fileWriter = new FileWriter("name.txt"); PrintWriter printWriter = new PrintWriter(fileWriter); printWriter.println(myName); printWriter.close(); Scanner sc = new Scanner(new File("name.txt")); System.out.println(sc.nextLine()); sc.close(); } }
ronaldo
Get Answers For Free
Most questions answered within 1 hours.