// Show how to write to a file using a PrintWriter object. import java.io.PrintWriter; import java.io.FileNotFoundException; public class WriteToFile { public static void main(String[ ] args) throws FileNotFoundException { // Declare and instantiate PrintWriter object. PrintWriter pw = new PrintWriter("out.txt"); // Write to output file using println method. pw.println("This is a test."); // Write to output file using printf method. int x = 5, y = 6; pw.printf("%d + %d = %d", x, y, x + y); // PrintWriter object must be closed to flush output buffer. pw.close( ); } }