File file = new File("validFile.txt"); Scanner fileReader = null; try { fileReader = new Scanner(file); } catch(FileNotFoundException e) { System.exit(1); } while(fileReader.hasNext()) { //code here }