Skip to content

Commit 8590e3b

Browse files
authored
Merge pull request #352 from habibasorour/issue_335_listing
Issue 335: 8.4 listing
2 parents 6046a67 + b56a824 commit 8590e3b

1 file changed

Lines changed: 79 additions & 37 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,11 @@ public class CreateFile {
255255
</p>
256256

257257
<p>
258-
Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
258+
<xref ref="write-file-class-java" text="type-global"/> shows the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>.
259259
</p>
260-
261-
<program xml:id="write-file-class-java" language="java">
260+
261+
<listing xml:id="write-file-class-java">
262+
<program language="java">
262263
import java.io.File;
263264
import java.io.FileWriter;
264265
import java.io.IOException;
@@ -269,39 +270,46 @@ public class CreateFile {
269270
}
270271
}
271272
</program>
273+
</listing>
272274

273275
<p>
274-
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. The equivalent Python code to this operation is:
276+
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. <xref ref="filerwriter-object-python" text="type-global"/> shows the code to create a <c>myWriter</c> object in Python.
275277
</p>
276-
277-
<program xml:id="filerwriter-object-python" language="python">
278+
<listing xml:id="filerwriter-object-python">
279+
<program language="python">
278280
with open("myfile.txt", "w") as myWriter:
279281
</program>
282+
</listing>
280283

281284
<p>
282-
The Java code to create a <c>FileWriter</c> object is:
285+
<xref ref="filerwriter-object-java" text="type-global"/> shows the Java code to create a <c>FileWriter</c> object. Note that the <c>FileWriter</c> object is created with the name of the file to write to as an argument. If the file does not exist, it will be created. If it does exist, it will be overwritten.:
283286
</p>
284287

285-
<program xml:id="filerwriter-object-java" language="java">
288+
<listing xml:id="filerwriter-object-java">
289+
<program language="java">
286290
FileWriter myWriter = new FileWriter("myfile.txt");
287291
</program>
292+
</listing>
288293

289294
<p>
290-
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:
295+
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. The <xref ref="write-and-close-python" text="type-global"/> shows the code to write to a file in Python.
291296
</p>
292-
293-
<program xml:id="write-and-close-python" language="python">
297+
<listing xml:id="write-and-close-python">
298+
<program language="python">
294299
my_writer.write("File successfully updated!")
295300
</program>
301+
</listing>
296302

297303
<p>
298-
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
304+
<xref ref="write-and-close-java" text="type-global"/> shows the Java equivalent. This is almost completely identical except for the second line, which is very important!
299305
</p>
300306

301-
<program xml:id="write-and-close-java" language="java">
307+
<listing xml:id="write-and-close-java">
308+
<program language="java">
302309
myWriter.write("File successfully updated!");
303310
myWriter.close();
304311
</program>
312+
</listing>
305313

306314
<note>
307315
<p>
@@ -310,10 +318,11 @@ public class CreateFile {
310318
</note>
311319

312320
<p>
313-
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:
321+
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. <xref ref="file-try-catch-python" text="type-global"/> shows the code to write to a file in Python.
314322
</p>
315323

316-
<program xml:id="file-try-catch-python" language="python" add-files = "my-file-8-4-2">
324+
<listing xml:id="file-try-catch-python">
325+
<program language="python" add-files = "my-file-8-4-2">
317326
try:
318327
with open("myfile.txt", "w") as my_writer:
319328
my_writer.write("File successfully updated!")
@@ -323,12 +332,14 @@ public class CreateFile {
323332
import traceback
324333
traceback.print_exc()
325334
</program>
335+
</listing>
326336

327337
<p>
328-
And the equivalent Java code:
338+
<xref ref="file-try-catch-java" text="type-global"/> shows the Java equivalent.
329339
</p>
330340

331-
<program language="java" xml:id="file-try-catch-java">
341+
<listing xml:id="file-try-catch-java">
342+
<program language="java">
332343
try {
333344
FileWriter myWriter = new FileWriter("myfile.txt");
334345
myWriter.write("File successfully updated!");
@@ -339,16 +350,23 @@ public class CreateFile {
339350
e.printStackTrace();
340351
}
341352
</program>
353+
</listing>
342354

343355
<p>
344-
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
356+
And that's it! We will add our code to the foundational code for a complete program. First, <xref ref="file-write-full-python" text="type-global"/> shows the completed Python code.
345357
</p>
346-
<datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes">
358+
359+
<data xml:id="listing-data-file-write-full-python">
360+
<title>Data file for writing example</title>
361+
<datafile xml:id="data-file-write-full-python" label="data-file-write-full-python" filename="myfile.txt" editable="yes">
347362
<pre>
348363

349364
</pre>
350365
</datafile>
351-
<program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4">
366+
</data>
367+
368+
<listing xml:id="file-write-full-python">
369+
<program interactive="activecode" language="python" add-files="data-file-write-full-python">
352370
<code>
353371
try:
354372
with open("myfile.txt", "w") as my_writer:
@@ -360,16 +378,22 @@ public class CreateFile {
360378
traceback.print_exc()
361379
</code>
362380
</program>
381+
</listing>
363382

364383
<p>
365-
The completed Java code:
384+
<xref ref="file-write-full-java" text="type-global"/> shows the completed Java code.
366385
</p>
367-
<datafile label="my-file-8-4-5" filename="myfile.txt" xml:id= "my-file-8-4-5" editable="yes">
386+
<data xml:id="listing-data-file-write-full-java">
387+
<title>Data file for writing example</title>
388+
<datafile xml:id="data-file-write-full-java" label="data-file-write-full-java" filename="myfile.txt" editable="yes">
368389
<pre>
369390

370391
</pre>
371392
</datafile>
372-
<program xml:id="file-write-full-java" interactive="activecode" language="java" add-files = "my-file-8-4-5">
393+
</data>
394+
395+
<listing xml:id="file-write-full-java">
396+
<program interactive="activecode" language="java" add-files = "data-file-write-full-java">
373397
<code>
374398
import java.io.FileWriter;
375399
import java.io.IOException;
@@ -387,8 +411,10 @@ public class CreateFile {
387411
}
388412
}
389413
}
390-
</code>
414+
</code> ~
391415
</program>
416+
</listing>
417+
392418

393419
<note>
394420
<p>
@@ -397,22 +423,30 @@ public class CreateFile {
397423
</note>
398424

399425
<p>
400-
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:
426+
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 as shown in <xref ref="append-true" text="type-global"/>.
401427
</p>
402428

403-
<program xml:id="append-true" language="java">
429+
<listing xml:id="append-true">
430+
<program language="java">
404431
FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
405432
</program>
433+
</listing>
406434

407435
<p>
408-
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:
436+
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 as shown in <xref ref="file-write-with-append" text="type-global"/>.
409437
</p>
410-
<datafile label="my-file-8-4-6" filename="myfile" xml:id= "my-file-8-4-6" editable="yes">
438+
439+
<data xml:id="listing-data-file-write-with-append">
440+
<title>Data file for writing example</title>
441+
<datafile xml:id="data-file-write-with-append" label="data-file-write-with-append" filename="myfile" editable="yes">
411442
<pre>
412443

413444
</pre>
414445
</datafile>
415-
<program xml:id="file-write-with-append" interactive="activecode" language="java" add-files = "my-file-8-4-6">
446+
</data>
447+
448+
<listing xml:id="file-write-with-append">
449+
<program interactive="activecode" language="java" add-files = "data-file-write-with-append">
416450
<code>
417451
import java.io.FileWriter;
418452
import java.io.IOException;
@@ -432,32 +466,40 @@ public class CreateFile {
432466
}
433467
</code>
434468
</program>
469+
</listing>
435470

436471
<p>
437-
Then if we run the program twice, the contents of <c>myfile.txt</c> would be:
472+
Then if we run the program twice, the contents of <c>myfile.txt</c> would be as <xref ref="file-write-with-append-output" text="type-global"/> shows.
438473
</p>
439474

440-
<pre>
475+
<listing xml:id="file-write-with-append-output">
476+
<program>
441477
File successfully updated!File successfully updated!
442-
</pre>
478+
</program>
479+
</listing>
443480

444481
<p>
445-
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:
482+
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 as <xref ref="file-write-with-newline" text="type-global"/> shows.
446483
</p>
447484

448-
<program xml:id="newline" language="java">
485+
<listing xml:id="file-write-with-newline">
486+
<program language="java">
449487
myWriter.write("File successfully updated!\n"); // Added newline character
450488
myWriter.close();
451489
</program>
490+
</listing>
452491

453492
<p>
454-
Running the code with the newline character twice will result in the following contents in myfile.txt:
493+
Running the code with the newline character twice will result in the following contents in myfile.txt as <xref ref="file-write-with-newline-output" text="type-global"/> shows.
455494
</p>
456495

457-
<pre>
496+
<listing xml:id="file-write-with-newline-output">
497+
<program>
458498
File successfully updated!
459499
File successfully updated!
460-
</pre>
500+
</program>
501+
</listing>
502+
461503
</section>
462504

463505
<section xml:id="file-delete">

0 commit comments

Comments
 (0)