Skip to content

Commit ffaf7dd

Browse files
committed
added listing tags
1 parent c116c55 commit ffaf7dd

1 file changed

Lines changed: 47 additions & 16 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,9 @@ public class CreateFile {
242242
<p>
243243
Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
244244
</p>
245-
246-
<program xml:id="write-file-class-java" language="java">
245+
246+
<listing xml:id="write-file-class-java">
247+
<program language="java">
247248
import java.io.File;
248249
import java.io.FileWriter;
249250
import java.io.IOException;
@@ -254,39 +255,46 @@ public class CreateFile {
254255
}
255256
}
256257
</program>
258+
</listing>
257259

258260
<p>
259261
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. The equivalent Python code to this operation is:
260262
</p>
261-
262-
<program xml:id="filerwriter-object-python" language="python">
263+
<listing xml:id="filerwriter-object-python">
264+
<program language="python">
263265
with open("myfile.txt", "w") as myWriter:
264266
</program>
267+
</listing>
265268

266269
<p>
267270
The Java code to create a <c>FileWriter</c> object is:
268271
</p>
269272

270-
<program xml:id="filerwriter-object-java" language="java">
273+
<listing xml:id="filerwriter-object-java">
274+
<program language="java">
271275
FileWriter myWriter = new FileWriter("myfile.txt");
272276
</program>
277+
</listing>
273278

274279
<p>
275280
In this next step, we will use the <c>write()</c> method from the FileWriter class. This method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types. First, how this step is completed with Python:
276281
</p>
277-
278-
<program xml:id="write-and-close-python" language="python">
282+
<listing xml:id="write-and-close-python">
283+
<program language="python">
279284
my_writer.write("File successfully updated!")
280285
</program>
286+
</listing>
281287

282288
<p>
283289
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
284290
</p>
285291

286-
<program xml:id="write-and-close-java" language="java">
292+
<listing xml:id="write-and-close-java">
293+
<program language="java">
287294
myWriter.write("File successfully updated!");
288295
myWriter.close();
289296
</program>
297+
</listing>
290298

291299
<note>
292300
<p>
@@ -298,7 +306,8 @@ public class CreateFile {
298306
Next, we will again add the required try/catch blocks utilizing the <c>IOException</c> class. Just like with creating files, the program will not compile without these crucial additions! We will also add some print statements to inform us of the success of the file write operation. First, a Python example:
299307
</p>
300308

301-
<program xml:id="file-try-catch-python" language="python" add-files = "my-file-8-4-2">
309+
<listing xml:id="file-try-catch-python">
310+
<program language="python" add-files = "my-file-8-4-2">
302311
try:
303312
with open("myfile.txt", "w") as my_writer:
304313
my_writer.write("File successfully updated!")
@@ -308,12 +317,14 @@ public class CreateFile {
308317
import traceback
309318
traceback.print_exc()
310319
</program>
320+
</listing>
311321

312322
<p>
313323
And the equivalent Java code:
314324
</p>
315325

316-
<program language="java" xml:id="file-try-catch-java">
326+
<listing xml:id="file-try-catch-java">
327+
<program language="java">
317328
try {
318329
FileWriter myWriter = new FileWriter("myfile.txt");
319330
myWriter.write("File successfully updated!");
@@ -324,16 +335,22 @@ public class CreateFile {
324335
e.printStackTrace();
325336
}
326337
</program>
338+
</listing>
327339

328340
<p>
329341
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
330342
</p>
343+
<data xml:id="my-file-8-4-2">
344+
<title>Data file for writing example</title>
331345
<datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes">
332346
<pre>
333347

334348
</pre>
335349
</datafile>
336-
<program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4">
350+
</data>
351+
352+
<listing xml:id="file-write-full-python">
353+
<program interactive="activecode" language="python" add-files="my-file-8-4-4">
337354
<code>
338355
try:
339356
with open("myfile.txt", "w") as my_writer:
@@ -345,10 +362,13 @@ public class CreateFile {
345362
traceback.print_exc()
346363
</code>
347364
</program>
365+
</listing>
348366

349367
<p>
350368
The completed Java code:
351369
</p>
370+
<data xml:id="my-file-8-4-5">
371+
<title>Data file for writing example</title>
352372
<datafile label="my-file-8-4-5" filename="myfile.txt" xml:id= "my-file-8-4-5" editable="yes">
353373
<pre>
354374

@@ -374,6 +394,7 @@ public class CreateFile {
374394
}
375395
</code>
376396
</program>
397+
</data>
377398

378399
<note>
379400
<p>
@@ -385,19 +406,26 @@ public class CreateFile {
385406
Speaking of overwriting data, what if we want to append text to the end of any text already in <c>myfile.txt</c>? To accomplish this, we can pass a <c>boolean</c> argument along with the file name when creating a new data argument:
386407
</p>
387408

388-
<program xml:id="append-true" language="java">
409+
<listing xml:id="append-true">
410+
<program language="java">
389411
FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
390412
</program>
413+
</listing>
391414

392415
<p>
393416
Now, when we use <c>write()</c> method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the <c>boolean</c> argument:
394417
</p>
395-
<datafile label="my-file-8-4-6" filename="myfile" xml:id= "my-file-8-4-6" editable="yes">
418+
<data xml:id="my-file-8-4-6">
419+
<title>Data file for writing example</title>
420+
<datafile label="my-file-8-4-6" filename="myfile" editable="yes">
396421
<pre>
397422

398423
</pre>
399424
</datafile>
400-
<program xml:id="file-write-with-append" interactive="activecode" language="java" add-files = "my-file-8-4-6">
425+
</data>
426+
427+
<listing xml:id="file-write-with-append">
428+
<program interactive="activecode" language="java" add-files = "my-file-8-4-6">
401429
<code>
402430
import java.io.FileWriter;
403431
import java.io.IOException;
@@ -417,6 +445,7 @@ public class CreateFile {
417445
}
418446
</code>
419447
</program>
448+
</listing>
420449

421450
<p>
422451
Then if we run the program twice, the contents of <c>myfile.txt</c> would be:
@@ -430,15 +459,17 @@ public class CreateFile {
430459
This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the <c>\n</c> newline character:
431460
</p>
432461

433-
<program xml:id="newline" language="java">
462+
<listing xml:id="file-write-with-newline">
463+
<program language="java">
434464
myWriter.write("File successfully updated!\n"); // Added newline character
435465
myWriter.close();
436466
</program>
467+
</listing>
437468

438469
<p>
439470
Running the code with the newline character twice will result in the following contents in myfile.txt:
440471
</p>
441-
472+
442473
<pre>
443474
File successfully updated!
444475
File successfully updated!

0 commit comments

Comments
 (0)