Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions source/ch8_filehandling.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ public class CreateFile {
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.
</p>
<p>
Consider the following Python code example that reads each line of the file and prints it to the console.
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.
</p>
<datafile label="myfile-read" filename="myfile.txt" xml:id= "myfile-read" editable="no">
<data xml:id="myfile-read">
<title>Data file for reading example</title>
<datafile label="myfile-read" filename="myfile.txt" editable="no">
<pre>
1
2
Expand All @@ -177,8 +179,9 @@ public class CreateFile {
8
</pre>
</datafile>

<program xml:id="file-read-python" interactive="activecode" language="python" add-files="myfile-read">
</data>
<listing xml:id="file-read-python">
<program interactive="activecode" language="python" add-files="myfile-read">
<code>
filename = "myfile.txt"
try:
Expand All @@ -192,12 +195,14 @@ public class CreateFile {
print("file could not be opened")
</code>
</program>
</listing>

<p>
The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, in Java we use the <c>Scanner</c> object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
<xref ref="file-read-java" text="type-global"/> functions very similarly to <xref ref="file-read-python" text="type-global"/>. The main difference here is that unlike Python, in Java we use the <c>Scanner</c> object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
</p>

<program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read">
<listing xml:id="file-read-java">
<program interactive="activecode" language="java" add-files= "myfile-read">
<code>
import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -218,6 +223,7 @@ public class CreateFile {
}
</code>
</program>
</listing>
<p>
You may have noticed that there are some new methods you haven't seen yet. The <c>hasNextLine()</c> method checks if there is a next line in the file, and returns <c>false</c> if there isn't. This method allows us to iterate over every line till there is no next line. The <c>nextLine()</c> method of the Scanner object returns the next line in the file as a string.
</p>
Expand Down