diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index 7a7cf33..d9656b0 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -287,6 +287,44 @@ public class CreateFile {

You may have noticed that there are some new methods you haven't seen yet. The hasNextLine() method checks if there is a next line in the file, and returns false if there isn't. This method allows us to iterate over every line till there is no next line. The nextLine() method of the Scanner object returns the next line in the file as a string.

+ + + +

+ Construct the part of a Java program that opens a file with a Scanner and + prints each line until there are no more lines. Drag the blocks into the correct order on the right. +

+
+ + + + try (Scanner fileReader = new Scanner(new File(filename))) { + + + try (Scanner fileReader = new Scanner(filename)) { + + + + + + while (fileReader.hasNextLine()) { + + + while (fileReader.nextLine()) { + + + + + String data = fileReader.nextLine(); + System.out.println(data); + + + + } + } + + +