Java File I/O
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.
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class NameFile { public static void main(String[] args) throws FileNotFoundException { String filename = "name.txt"; File file = new File(filename); PrintWriter pw = new PrintWriter(file); pw.println("Cristiano Ronaldo"); pw.close(); Scanner fin = new Scanner(file); System.out.println(fin.nextLine()); fin.close(); } }
Get Answers For Free
Most questions answered within 1 hours.