When you execute new Scanner(new File("data.txt")), you're trying to read from a file named data.txt but you haven't yet created a file named data.txt. Java can't find the file to read from, so the program crashes.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;
class RandomNumbers {
public static void main(String args[]) throws FileNotFoundException {
Random random = new Random();
PrintStream diskWriter = new PrintStream("randomNumbers.txt");
int i = 1;
while (i <= 10) {
diskWriter.println(random.nextInt());
i++;
}
diskWriter.close();
Scanner diskScanner = new Scanner(new File("randomNumbers.txt"));
i = 1;
while (i <= 10) {
System.out.println(diskScanner.nextInt());
i++;
}
diskScanner.close();
}
}