// Show how to load data from a file // into an array. The filename is hardcoded // as a path relative to the current folder. import java.util.Scanner; import java.io.*; public class LoadFromFile { // The code will not compile unless the main method // is allowed to throw a FileNotFoundException. public static void main(String[ ] argv) throws FileNotFoundException { // Declare and instantiate an array containing all zeros. int[ ] a = new int[10]; // Create FileReader object that reads from a hard drive file. FileReader fr = new FileReader("numbers.txt"); // Create Scanner object that reads from FileReader object. Scanner s = new Scanner(fr); // Read ints until there are no more ints left in file, // loading the ints into an array. for (int i = 0; i < a.length && s.hasNextInt( ); i++) a[i] = s.nextInt( ); // Print out resulting array for (int i = 0; i < a.length; i++) System.out.println(a[i]); } }