1  import java.io.File;
  2  import java.io.FileNotFoundException;
  3  import java.io.PrintWriter;
  4  import java.util.Scanner;
  5  
  6  /**
  7     This program reads a file with numbers, and writes the numbers to another
  8     file, lined up in a column and followed by their total. 
  9  */
 10  public class Total
 11  {
 12     public static void main(String[] args) throws FileNotFoundException
 13     {
 14        // Prompt for the input and output file names
 15  
 16        Scanner console = new Scanner(System.in);
 17        System.out.print("Input file: ");
 18        String inputFileName = console.next();
 19        System.out.print("Output file: ");
 20        String outputFileName = console.next();
 21  
 22        // Construct the Scanner and PrintWriter objects for reading and writing
 23  
 24        File inputFile = new File(inputFileName);
 25        Scanner in = new Scanner(inputFile);
 26        PrintWriter out = new PrintWriter(outputFileName);
 27  
 28        // Read the input and write the output
 29  
 30        double total = 0;
 31        
 32        while (in.hasNextDouble())
 33        {
 34           double value = in.nextDouble();
 35           out.printf("%15.2f\n", value);
 36           total = total + value;
 37        }
 38  
 39        out.printf("Total: %8.2f\n", total);
 40  
 41        in.close();
 42        out.close();
 43     }
 44  }