Skip to content

Commit f33f44b

Browse files
committed
add comments to code
1 parent c116c55 commit f33f44b

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,18 @@ public class CreateFile {
208208
<program interactive="activecode" language="java" add-files= "myfile-read">
209209
<code>
210210
import java.io.File;
211-
import java.io.FileNotFoundException;
211+
import java.io.FileNotFoundException; // This import is necessary to handle the exception if the file is not found
212212
import java.util.Scanner;
213213
public class ReadFile {
214214
public static void main (String[] args) {
215215
String filename = "myfile.txt";
216-
try (Scanner fileReader = new Scanner(new File(filename))) {
217-
while (fileReader.hasNextLine()) {
216+
try (Scanner fileReader = new Scanner(new File(filename))) { // try to open the file and create a Scanner object
217+
while (fileReader.hasNextLine()) { // while there is a next line in the file
218218
String data = fileReader.nextLine();
219219
System.out.println(data);
220220
}
221221
}
222-
catch (FileNotFoundException e) {
222+
catch (FileNotFoundException e) { // and catch the exception if the file is not found
223223
System.out.println("Error: The file '" + filename + "' was not found.");
224224
}
225225
}

0 commit comments

Comments
 (0)