Skip to content

Commit 7287ff6

Browse files
committed
replace depreciated input with code
1 parent 1938c22 commit 7287ff6

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
File handling is an integral part of programming. Most programming languages have the ability to create, read from, write to, and delete, files. In Python, most built-in libraries are available without needing to explicitly import additional packages, but some libraries like <c>math</c> do need to be imported. Consider the following.
1111
</p>
1212
<program xml:id = "file-class-import-Python-example" interactive="activecode" language="python">
13-
<input>
13+
<code>
1414
import math
1515
print(math.sqrt(25))
16-
</input>
16+
</code>
1717
</program>
1818

1919
<p>
2020
Delete the first line that says <c>import math</c> and see what happens. The <c>import math</c> is needed. The same program in Java would look like this:
2121
</p>
2222
<program xml:id = "file-class-import-Java-example" interactive="activecode" language="java">
23-
<input>
23+
<code>
2424
import java.lang.Math;
2525

2626
public class SquareRoot {
2727
public static void main(String[] args) {
2828
System.out.println(Math.sqrt(25));
2929
}
3030
}
31-
</input>
31+
</code>
3232
</program>
3333
<p>
3434
Note the use of <c>import java.lang.Math;</c> in the above to import the <c>Math</c> class. Unlike Python, Java requires explicit <c>import</c> for most libraries, including the <c>Math</c> class and many classes related to file handling.
@@ -107,7 +107,7 @@ public class CreateFile {
107107
</p>
108108

109109
<program xml:id="create-file-python" interactive="activecode" language="python">
110-
<input>
110+
<code>
111111
filename = "newfile.txt"
112112
print("Attempting to write to '" + filename + "' using 'w' mode...")
113113
try:
@@ -118,15 +118,15 @@ public class CreateFile {
118118
# This would only catch other unexpected errors
119119
print("An unexpected error occurred during write: " + str(e))
120120

121-
</input>
121+
</code>
122122
</program>
123123

124124
<p>
125125
Now, let's look at Java code that accomplishes the same task:
126126
</p>
127127

128128
<program xml:id="create-file-java" interactive="activecode" language="java">
129-
<input>
129+
<code>
130130
import java.io.File;
131131
import java.io.IOException;
132132

@@ -146,7 +146,7 @@ public class CreateFile {
146146
}
147147
}
148148
}
149-
</input>
149+
</code>
150150
</program>
151151

152152
<note>
@@ -179,7 +179,7 @@ public class CreateFile {
179179
</datafile>
180180

181181
<program xml:id="file-read-python" interactive="activecode" language="python" add-files="myfile-read">
182-
<input>
182+
<code>
183183
filename = "myfile.txt"
184184
try:
185185
# Attempt to open the file in read mode ('r')
@@ -190,15 +190,15 @@ public class CreateFile {
190190
except:
191191
#catches if the file doesn't exist or can't be written to
192192
print("file could not be opened")
193-
</input>
193+
</code>
194194
</program>
195195

196196
<p>
197197
The following Java code functions very similarly to the previous Python. The main difference here is that unlike Python, in Java we use the <c>Scanner</c> object to iterate through and read lines in the file. You will notice that the structure of the Java code is still similar to the Python; Both use a try and catch statement to read the file and catch any errors.
198198
</p>
199199

200200
<program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read">
201-
<input>
201+
<code>
202202
import java.io.File;
203203
import java.io.FileNotFoundException;
204204
import java.util.Scanner;
@@ -216,7 +216,7 @@ public class CreateFile {
216216
}
217217
}
218218
}
219-
</input>
219+
</code>
220220
</program>
221221
<p>
222222
You may have noticed that there are some new methods you haven't seen yet. The <c>hasNextLine()</c> method checks if there is a next line in the file, and returns <c>false</c> if there isn't. This method allows us to iterate over every line till there is no next line. The <c>nextLine()</c> method of the Scanner object returns the next line in the file as a string.
@@ -325,7 +325,7 @@ public class CreateFile {
325325
</pre>
326326
</datafile>
327327
<program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4">
328-
<input>
328+
<code>
329329
try:
330330
with open("myfile.txt", "w") as my_writer:
331331
my_writer.write("File successfully updated!")
@@ -334,7 +334,7 @@ public class CreateFile {
334334
print("An error occurred.")
335335
import traceback
336336
traceback.print_exc()
337-
</input>
337+
</code>
338338
</program>
339339

340340
<p>
@@ -346,7 +346,7 @@ public class CreateFile {
346346
</pre>
347347
</datafile>
348348
<program xml:id="file-write-full-java" interactive="activecode" language="java" add-files = "my-file-8-4-5">
349-
<input>
349+
<code>
350350
import java.io.FileWriter;
351351
import java.io.IOException;
352352

@@ -363,7 +363,7 @@ public class CreateFile {
363363
}
364364
}
365365
}
366-
</input>
366+
</code>
367367
</program>
368368

369369
<note>
@@ -389,7 +389,7 @@ public class CreateFile {
389389
</pre>
390390
</datafile>
391391
<program xml:id="file-write-with-append" interactive="activecode" language="java" add-files = "my-file-8-4-6">
392-
<input>
392+
<code>
393393
import java.io.FileWriter;
394394
import java.io.IOException;
395395

@@ -406,7 +406,7 @@ public class CreateFile {
406406
}
407407
}
408408
}
409-
</input>
409+
</code>
410410
</program>
411411

412412
<p>
@@ -444,7 +444,7 @@ public class CreateFile {
444444
</p>
445445

446446
<program xml:id="create-file-to-delete-java" interactive="activecode" language="java">
447-
<input>
447+
<code>
448448
import java.io.File;
449449
import java.io.IOException;
450450

@@ -464,7 +464,7 @@ public class CreateFile {
464464
}
465465
}
466466
}
467-
</input>
467+
</code>
468468
</program>
469469

470470
<p>
@@ -490,7 +490,7 @@ public class CreateFile {
490490

491491

492492
<program xml:id="delete-file-java" interactive="activecode" language="java">
493-
<input>
493+
<code>
494494
import java.io.File;
495495

496496
public class DeleteFile {
@@ -503,7 +503,7 @@ public class CreateFile {
503503
}
504504
}
505505
}
506-
</input>
506+
</code>
507507
</program>
508508

509509
<p>

0 commit comments

Comments
 (0)