Skip to content

Commit 5a5573d

Browse files
authored
Merge pull request #119 from coco3427/ch8code
Ch8code needs testing
2 parents d9482ae + 370f9d8 commit 5a5573d

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<p>
4545
Java includes a class called <c>File</c> in the <c>io</c> library. The class can be imported with the following line. Be sure to capitalize <c>File</c>.
4646
</p>
47-
<program xml:id = "file-class-import-example" interactive="activecode" language="java">
47+
<program xml:id = "file-class-import-io-example" interactive="activecode" language="java">
4848
<code>
4949
import java.io.File;
5050
import java.io.IOException;public class Main {
@@ -94,14 +94,15 @@
9494
<p>
9595
We will now create a <c>File</c> object. It is important to create a meaningful name for the <c>File</c> object. We will call ours <c>myFile</c>.
9696
</p>
97-
<datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="no" include-source="yes">
97+
<datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes">
9898
<pre>
9999
empty file
100100
</pre>
101101
</datafile>
102-
<program interactive="activecode" language="java">
102+
<program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1">
103103
<code>
104-
import java.io.File;public class Main {
104+
import java.io.File;
105+
public class Main {
105106
public static void main(String[] args) {
106107
File myFile = new File("myfile.txt");
107108
System.out.println(myFile);
@@ -123,12 +124,8 @@
123124
<p>
124125
First, lets look at the equivalent Python code:
125126
</p>
126-
<datafile label="my-file-8-2-2" filename="myfile.txt" xml:id= "my-file-8-2-2" editable="no" include-source="yes">
127-
<pre>
128-
empty file
129-
</pre>
130-
</datafile>
131-
<program interactive="activecode" language="python">
127+
128+
<program xml:id="creat-file-exp-2" interactive="activecode" language="python">
132129
<code>
133130
filename = "newfile.txt"
134131
print(f"Attempting to write to '{filename}' using 'w' mode...")
@@ -145,12 +142,12 @@
145142
<p>
146143
Now, let's look at Java code that accomplishes the same task:
147144
</p>
148-
<datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="no" include-source="yes">
145+
<datafile label="my-file-8-2-3" filename="myfile.txt" xml:id= "my-file-8-2-3" editable="yes">
149146
<pre>
150147
empty file
151148
</pre>
152149
</datafile>
153-
<program interactive="activecode" language="java">
150+
<program xml:id="create-file-exp3" interactive="activecode" language="java" add-files="my-file-8-2-3">
154151
<code>
155152
import java.io.File;
156153
import java.io.IOException;
@@ -181,7 +178,7 @@
181178
<p>
182179
Let's take a look at how we can use Java to read file contents. We'll start again with library imports and building a class, this time importing the Scanner and FileNotFoundException classes. We will call this class ReadFile:
183180
</p>
184-
<datafile label="my-file" filename="myfile.txt" xml:id= "my-file" editable="no" include-source="yes">
181+
<datafile label="my-file" filename="myfile.txt" xml:id= "my-file" editable="no">
185182
<pre>
186183
1
187184
2
@@ -195,7 +192,7 @@
195192
</datafile>
196193

197194

198-
<program interactive="activecode" language="java" add-files= "my-file">
195+
<program xml:id="file-read-exp1" interactive="activecode" language="java" add-files= "my-file">
199196
<code>
200197
import java.io.File;
201198
import java.io.FileNotFoundException;
@@ -223,7 +220,7 @@
223220
We will then create a new File object exactly the same as the one from the section on creating files. Additionally, we will create a Scanner object. The Scanner object is the object that does the file reading. We will call this scanner fileReader:
224221
</p>
225222

226-
<program interactive="activecode" language="java">
223+
<program xml:id="file-read-exp2" interactive="activecode" language="java">
227224
<code>
228225
import java.io.File;
229226
import java.io.FileNotFoundException;
@@ -251,9 +248,9 @@
251248
The next lines consists of a Python code example that reads each line of the file passed to the Scanner object.:
252249
</p>
253250

254-
<program interactive="activecode" language="python">
251+
<program xml:id="file-read-exp3" interactive="activecode" language="python" add-files="my-file">
255252
<code>
256-
with open("filename.txt", "r") as file_reader:
253+
with open("myfile.txt", "r") as file_reader:
257254
for line in file_reader:
258255
print(line.strip())
259256
</code>
@@ -263,7 +260,7 @@
263260
The equivalent Java code:
264261
</p>
265262

266-
<program interactive="activecode" language="java" add-files= "my-file">
263+
<program xml:id="file-read-exp4" interactive="activecode" language="java" add-files= "my-file">
267264
<code>
268265
import java.io.File;
269266
import java.io.FileNotFoundException;
@@ -292,7 +289,7 @@
292289
Alternatively, the following code can be used to store the all lines of myfile.txt to one variable:
293290
</p>
294291

295-
<program interactive="activecode" language="java">
292+
<program xml:id="file-read-exp5" interactive="activecode" language="java" add-files="my-file">
296293
<code>
297294
import java.io.File;
298295
import java.io.FileNotFoundException;
@@ -324,7 +321,7 @@
324321
Using the second method of storing all file contents to one file, the resulting full code including try/catch blocks (this time using FileNotFoundException instead of IOException) will look something like this. First, the Python code:
325322
</p>
326323

327-
<program interactive="activecode" language="python">
324+
<program xml:id="file-read-exp6" interactive="activecode" language="python">
328325
<code>
329326
try:
330327
with open("myfile.txt", "r") as file_reader:
@@ -344,7 +341,7 @@
344341
And the Java equivalent:
345342
</p>
346343

347-
<program interactive="activecode" language="java">
344+
<program xml:id="file-read-exp7" interactive="activecode" language="java" add-files="my-file">
348345
<code>
349346
import java.io.File;
350347
import java.io.FileNotFoundException;
@@ -385,7 +382,7 @@
385382
Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
386383
</p>
387384

388-
<program interactive="activecode" language="java">
385+
<program xml:id="file-write-exp1" interactive="activecode" language="java">
389386
<code>
390387
import java.io.File;
391388
import java.io.FileWriter;
@@ -447,7 +444,7 @@
447444
3
448445
</pre>
449446
</datafile>
450-
<program interactive="activecode" language="python" add-files = "my-file-8-4-2">
447+
<program xml:id="file-write-exp2" interactive="activecode" language="python" add-files = "my-file-8-4-2">
451448
<code>
452449
with open("myfile8-4-2.txt", "r") as file_reader:
453450
while True:
@@ -466,7 +463,7 @@
466463

467464
</pre>
468465
</datafile>
469-
<program interactive="activecode" language="java" add-files = "my-file-8-4-3">
466+
<program xml:id="file-write-exp3" interactive="activecode" language="java" add-files = "my-file-8-4-3">
470467
<code>
471468
import java.io.File;
472469
import java.io.IOException;
@@ -494,7 +491,7 @@
494491

495492
</pre>
496493
</datafile>
497-
<program interactive="activecode" language="python" add-files = "my-file-8-4-4">
494+
<program xml:id="file-write-exp4" interactive="activecode" language="python" add-files = "my-file-8-4-4">
498495
<code>
499496
try:
500497
with open("myfile.txt", "w") as my_writer:
@@ -515,7 +512,7 @@
515512

516513
</pre>
517514
</datafile>
518-
<program interactive="activecode" language="java" add-files = "my-file-8-4-5">
515+
<program xml:id="file-write-exp5" interactive="activecode" language="java" add-files = "my-file-8-4-5">
519516
<code>
520517
import java.io.FileWriter;
521518
import java.io.IOException;
@@ -562,7 +559,7 @@
562559

563560
</pre>
564561
</datafile>
565-
<program interactive="activecode" language="java" add-files = "my-file-8-4-6">
562+
<program xml:id="file-write-exp6" interactive="activecode" language="java" add-files = "my-file-8-4-6">
566563
<code>
567564
import java.io.FileWriter;
568565
import java.io.IOException;

0 commit comments

Comments
 (0)