Skip to content

Commit 7259324

Browse files
committed
Updated some code blocks so they are consistent. Added Python code examples to match some of the Java code examples.
1 parent a226ed0 commit 7259324

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@
8585
import java.io.File;
8686
public class CreateFile {
8787
public static void main(String[] args) {
88-
File myFile = new File("myfile.txt");
89-
System.out.println(myFile);
88+
9089
}
9190
}
9291
</input>
@@ -248,18 +247,34 @@
248247
</program>
249248

250249
<p>
251-
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>:
250+
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>. The equivalent Python code to this operation is:
251+
</p>
252+
253+
<program xml:id="write-and-close-python" language="python">
254+
with open("myfile.txt", "w") as myWriter:
255+
</program>
256+
257+
<p>
258+
The Java code to create a <c>FileWriter</c> object is:
252259
</p>
253260

254261
<program xml:id="filerwriter-object-java" language="java">
255262
FileWriter myWriter = new FileWriter("myfile.txt");
256263
</program>
257264

258265
<p>
259-
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:
266+
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:
260267
</p>
268+
269+
<program xml:id="write-and-close-python" language="python">
270+
my_writer.write("File successfully updated!")
271+
</program>
261272

262-
<program xml:id="write-and-clase-java" language="java">
273+
<p>
274+
And the Java equivalent. This is almost completely identical except for the second line, which is very important!
275+
</p>
276+
277+
<program xml:id="write-and-close-java" language="java">
263278
myWriter.write("File successfully updated!");
264279
myWriter.close();
265280
</program>
@@ -280,8 +295,7 @@
280295
3
281296
</pre>
282297
</datafile>
283-
<program xml:id="file-try-catch-python" interactive="activecode" language="python" add-files = "my-file-8-4-2">
284-
<input>
298+
<program xml:id="file-try-catch-python" language="python" add-files = "my-file-8-4-2">
285299
try:
286300
with open("myfile.txt", "w") as my_writer:
287301
my_writer.write("File successfully updated!")
@@ -290,17 +304,12 @@
290304
print("An error occurred.")
291305
import traceback
292306
traceback.print_exc()
293-
</input>
294307
</program>
295308

296309
<p>
297310
And the equivalent Java code:
298311
</p>
299-
<datafile label="my-file-8-4-3" filename="myfile8-4-3.txt" xml:id= "my-file-8-4-3" editable="yes">
300-
<pre>
301-
302-
</pre>
303-
</datafile>
312+
304313
<program language="java" xml:id="file-try-catch-java">
305314
try {
306315
FileWriter myWriter = new FileWriter("myfile.txt");
@@ -316,11 +325,11 @@
316325
<p>
317326
And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code:
318327
</p>
319-
<datafile label="my-file-8-4-4" filename="myfile.txt" xml:id= "my-file-8-4-4" editable="yes">
328+
<datafile label="my-file-8-4-4" filename="my-file-8-4-4" xml:id= "my-file-8-4-4" editable="yes">
320329
<pre>
321330

322331
</pre>
323-
</datafile>
332+
</datafile>
324333
<program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4">
325334
<input>
326335
try:
@@ -380,7 +389,7 @@
380389
<p>
381390
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:
382391
</p>
383-
<datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes">
392+
<datafile label="my-file-8-4-6" filename="my-file-8-4-6" xml:id= "my-file-8-4-6" editable="yes">
384393
<pre>
385394

386395
</pre>

0 commit comments

Comments
 (0)