import java.io.*; // sum all the integers in a file public class filetest3 { public static void main(String[] args) throws IOException { String fname; // name of the input file String dataline; // data value from file int sum = 0; // sum of the values // prompt and read the name of the file InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader keyboard = new BufferedReader(stdin); System.out.print("Enter the name of a file to process: "); fname = keyboard.readLine(); // open the data file FileReader freader = new FileReader(fname); BufferedReader inputFile = new BufferedReader(freader); // read until eof and sum the values dataline = inputFile.readLine(); while (dataline != null) { sum += Integer.parseInt(dataline); dataline = inputFile.readLine(); } inputFile.close(); // print the final sum System.out.println ("The sum = " + sum); } }