Simple Java I/O part I Using scanner 8-Nov-18
Scanner You can use the Scanner class to read from a file just like you read from the console. File file = new File(“Random"); That is very similar to your Python file code. Scanner scanner = new Scanner(file);
Reading firstLine = scanner.nextLine(); for (int i = 0; i < N; i++) { String information = scanner.nextLine(); }
Catching exceptions Java forces you to catch some file related exceptions try { File file = new File(“blabla.txt”); Scanner scanner = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); In the JavaExperiments folder on Dropbox, look at FileRead.java