import java.io.*; import java.util.*; public class PrintSum { private static Scanner openInput(String inputFileName) throws FileNotFoundException { if (inputFileName == null) throw new IllegalArgumentException(); File inputFile = new File(inputFileName); return new Scanner(inputFile); } private static Formatter openOutput(String outputFileName) throws FileNotFoundException { if (outputFileName == null) throw new IllegalArgumentException(); File outputFile = new File(outputFileName); return new Formatter(outputFile); } public static void main(String[] args) { try { Scanner input = openInput(args[0]); Formatter output = openOutput(args[1]); int sum = 0; while (input.hasNextInt()) { sum += input.nextInt(); } System.out.format("sum = %d", sum); output.format("sum = %d", sum); input.close(); output.close(); } catch (FileNotFoundException e) { System.out.println("Couldn't open file: " + e); } catch (Exception e2) { System.out.println("Something bad happened: " + e2); } } }