/* Double.java * Practice file input and output. * Read an input file of numbers. Double each number, and print * corresponding results to output file. */ import java.util.Scanner; import java.io.FileInputStream; // for reading files import java.io.FileNotFoundException; // in case file doesn't exist! import java.io.PrintStream; // for file output public class Double { public static void main(String [] args) throws FileNotFoundException { // Open both files. Scanner inFile = new Scanner(new FileInputStream("double.in")); PrintStream outFile = new PrintStream("double.out"); // Read all integers in input file. while(inFile.hasNextInt()) { int num = inFile.nextInt(); // Put the doubled value in the output file. outFile.printf("%d\n", 2 * num); } // By the way, it's generally considered good style and safe to // remember to close the files used for I/O. This will ensure that // all the output makes its way to the output file. inFile.close(); outFile.close(); } }