// Project: InputOutput // No modules defined. // Source code file: WriteGreetings.java // Read names of persons from input file names.txt, // then write greetings to each of them, each in // their own output file. import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class WriteGreetings { public static void main(String[] args) throws FileNotFoundException { // Declare PrintWriter reference variable. PrintWriter pw; // Write greeting to Larry in greeting.txt output file. pw = new PrintWriter("greeting.txt"); pw.println("Hello, Larry, how are you?"); pw.close( ); // Write greetings to persons, each in their own // output file. Scanner in = new Scanner(new File("names.txt")); while (in.hasNextLine( )) { String name = in.nextLine( ); String greeting = "Hello " + name + ", how are you?"; String fileName = name + ".txt"; pw = new PrintWriter(fileName); pw.println(greeting); pw.close( ); } in.close( ); } }