import java.util.Scanner; import java.io.*; /** Oct 10 * Output Files: 1. import java.io.* * 2. put "throws IOException" clause in header of method * 3. create an object for writing of type PrintWriter * 4. Now you can write using println * 5. When you're finished with a file, close the file */ public class TableofSquaresFiles { /** This class prints a table of the numbers from some number to 1 * another number, and their squares. * 1 1 * 2 4 * 3 9 */ public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); System.out.println("What's the highest value you want in the table?"); int limit = scanner.nextInt(); // Step 3: create a PrintWriter object and connect it to a file // on disk called table.txt. Note this java program is going to // create this file. PrintWriter oFile = new PrintWriter("DataFiles\\table.txt"); oFile.println("Table of Squares"); oFile.println("Number Number^2"); for (int i=10; i<=limit; i++) { // Step 4: can use println with PrintWriter object oFile.println(i + " " + i*i); } // Step 5 oFile.close(); } }