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
+79-37Lines changed: 79 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -255,10 +255,11 @@ public class CreateFile {
255
255
</p>
256
256
257
257
<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
+
<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>.
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>. <xrefref="filerwriter-object-python"text="type-global"/> shows the code to create a <c>myWriter</c> object in Python.
The Java code to create a <c>FileWriter</c> object is:
285
+
<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.:
FileWriter myWriter = new FileWriter("myfile.txt");
287
291
</program>
292
+
</listing>
288
293
289
294
<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 <xrefref="write-and-close-python"text="type-global"/> shows the code to write to a file in Python.
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
304
+
<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!
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. <xrefref="file-try-catch-python"text="type-global"/> shows the code to write to a file in Python.
FileWriter myWriter = new FileWriter("myfile.txt");
334
345
myWriter.write("File successfully updated!");
@@ -339,16 +350,23 @@ public class CreateFile {
339
350
e.printStackTrace();
340
351
}
341
352
</program>
353
+
</listing>
342
354
343
355
<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, <xrefref="file-write-full-python"text="type-global"/> shows the completed Python code.
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 <xrefref="append-true"text="type-global"/>.
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 <xrefref="file-write-with-append"text="type-global"/>.
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 <xrefref="file-write-with-newline"text="type-global"/> shows.
446
483
</p>
447
484
448
-
<programxml:id="newline"language="java">
485
+
<listingxml:id="file-write-with-newline">
486
+
<programlanguage="java">
449
487
myWriter.write("File successfully updated!\n"); // Added newline character
450
488
myWriter.close();
451
489
</program>
490
+
</listing>
452
491
453
492
<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 <xrefref="file-write-with-newline-output"text="type-global"/> shows.
0 commit comments