You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/ch8_filehandling.ptx
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -240,7 +240,7 @@ public class CreateFile {
240
240
</p>
241
241
242
242
<p>
243
-
Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
243
+
<xrefref="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>.
244
244
</p>
245
245
246
246
<listingxml:id="write-file-class-java">
@@ -258,7 +258,7 @@ public class CreateFile {
258
258
</listing>
259
259
260
260
<p>
261
-
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. The equivalent Python code to this operation is:
261
+
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. <xrefref="filerwriter-object-python"text="type-global"/> shows the code to create a <c>myWriter</c> object in Python.
262
262
</p>
263
263
<listingxml:id="filerwriter-object-python">
264
264
<programlanguage="python">
@@ -267,7 +267,7 @@ public class CreateFile {
267
267
</listing>
268
268
269
269
<p>
270
-
The Java code to create a <c>FileWriter</c> object is:
270
+
<xrefref="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.:
271
271
</p>
272
272
273
273
<listingxml:id="filerwriter-object-java">
@@ -277,7 +277,7 @@ public class CreateFile {
277
277
</listing>
278
278
279
279
<p>
280
-
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:
280
+
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 <xrefref="write-and-close-python"text="type-global"/> shows the code to write to a file in Python.
281
281
</p>
282
282
<listingxml:id="write-and-close-python">
283
283
<programlanguage="python">
@@ -286,7 +286,7 @@ public class CreateFile {
286
286
</listing>
287
287
288
288
<p>
289
-
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
289
+
<xrefref="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!
290
290
</p>
291
291
292
292
<listingxml:id="write-and-close-java">
@@ -303,7 +303,7 @@ public class CreateFile {
303
303
</note>
304
304
305
305
<p>
306
-
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:
306
+
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. <xrefref="file-try-catch-python"text="type-global"/> shows the code to write to a file in Python.
307
307
</p>
308
308
309
309
<listingxml:id="file-try-catch-python">
@@ -320,7 +320,7 @@ public class CreateFile {
320
320
</listing>
321
321
322
322
<p>
323
-
And the equivalent Java code:
323
+
<xrefref="file-try-catch-java"text="type-global"/> shows the Java equivalent.
324
324
</p>
325
325
326
326
<listingxml:id="file-try-catch-java">
@@ -338,7 +338,7 @@ public class CreateFile {
338
338
</listing>
339
339
340
340
<p>
341
-
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
341
+
And that's it! We will add our code to the foundational code for a complete program. First, <xrefref="file-write-full-python"text="type-global"/> shows the completed Python code.
342
342
</p>
343
343
<dataxml:id="my-file-8-4-2">
344
344
<title>Data file for writing example</title>
@@ -365,7 +365,7 @@ public class CreateFile {
365
365
</listing>
366
366
367
367
<p>
368
-
The completed Java code:
368
+
<xrefref="file-write-full-java"text="type-global"/> shows the completed Java code.
369
369
</p>
370
370
<dataxml:id="my-file-8-4-5">
371
371
<title>Data file for writing example</title>
@@ -403,7 +403,7 @@ public class CreateFile {
403
403
</note>
404
404
405
405
<p>
406
-
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:
406
+
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 <xrefref="append-true"text="type-global"/>.
407
407
</p>
408
408
409
409
<listingxml:id="append-true">
@@ -413,8 +413,8 @@ public class CreateFile {
413
413
</listing>
414
414
415
415
<p>
416
-
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:
417
-
</p>
416
+
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 <xrefref="file-write-with-append"text="type-global"/>.
Then if we run the program twice, the contents of <c>myfile.txt</c> would be:
451
+
Then if we run the program twice, the contents of <c>myfile.txt</c> would be as <xrefref="file-write-with-append-output"text="type-global"/> shows.
452
452
</p>
453
453
454
454
<listingxml:id="file-write-with-append-output">
@@ -458,7 +458,7 @@ public class CreateFile {
458
458
</listing>
459
459
460
460
<p>
461
-
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:
461
+
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 <xrefref="file-write-with-newline"text="type-global"/> shows.
462
462
</p>
463
463
464
464
<listingxml:id="file-write-with-newline">
@@ -469,7 +469,7 @@ public class CreateFile {
469
469
</listing>
470
470
471
471
<p>
472
-
Running the code with the newline character twice will result in the following contents in myfile.txt:
472
+
Running the code with the newline character twice will result in the following contents in myfile.txt as <xrefref="file-write-with-newline-output"text="type-global"/> shows.
0 commit comments