Skip to content

Commit 363513f

Browse files
authored
Merge pull request #348 from habibasorour/issue_334_comment
Issue 334: 8.3 add comments to code
2 parents c37c607 + 2560b42 commit 363513f

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ public class CreateFile {
166166
Let’s take a look at how we can use Python to understand how read file contents in Java. In order to read files generally you iterate through each line in the file and read the line's content. In Java, you read files in a very similar way, however in Java we will use the <c>Scanner</c> class in order to iterate through the lines.
167167
</p>
168168
<p>
169-
Consider <xref ref="file-read-python" text="type-global"/> that reads each line of <xref ref="myfile-read" text="type-global"/> and prints it to the console.
169+
Consider <xref ref="file-read-python" text="type-global"/> that reads each line of <xref ref="listing-myfile-read" text="type-global"/> and prints it to the console.
170170
</p>
171-
<data xml:id="myfile-read">
171+
<data xml:id="listing-myfile-read">
172172
<title>Data file for reading example</title>
173-
<datafile label="myfile-read" filename="myfile.txt" editable="no">
173+
<datafile xml:id= "myfile-read" label="myfile-read" filename="myfile.txt" editable="no">
174174
<pre>
175175
1
176176
2
@@ -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)