Skip to content

Commit a226ed0

Browse files
committed
changed all code tags to input tags in code blocks. Added or changed xml ids for all code.
1 parent 680f110 commit a226ed0

1 file changed

Lines changed: 47 additions & 47 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 47 additions & 47 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-
<code>
13+
<input>
1414
import math
1515
print(math.sqrt(25))
16-
</code>
16+
</input>
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-
<code>
23+
<input>
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-
</code>
31+
</input>
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.
@@ -42,30 +42,30 @@
4242
Much like the <c>Math</c> class, in order for your program to work with files you need use <c>import</c>. Java includes a class called <c>File</c> in the <c>io</c> library. This class allows you to create <c>File</c> objects, and use its public methods.
4343
</p>
4444

45-
<program language="java">
45+
<program xml:id="io-file-import" language="java">
4646
import java.io.File;
4747
</program>
4848
<p>
4949
The <c>Scanner</c> class from the <c>util</c> library will need to be imported if there is any need for a program to read a file. It should be noted that this library is unnecessary if the program will not be reading any data from a file.
5050
</p>
5151

52-
<program language="java">
52+
<program xml:id="util-scanner-import" language="java">
5353
import java.util.Scanner;
5454
</program>
5555

5656
<p>
5757
The <c>FileWriter</c> class can be used to write to files. In the same way that the <c>Scanner</c> class isn't needed unless the program will read from a file, the <c>FileWriter</c> class isn't needed unless the program will write to a file.
5858
</p>
5959

60-
<program language="java">
60+
<program xml:id="io-filewriter-import" language="java">
6161
import java.io.FileWriter;
6262
</program>
6363

6464
<p>
6565
Finally, these last two classes provide error handling and must be used in tandem with the <c>File</c> class when reading from or writing to files. <c>IOException</c> handles file creation and writing errors, while <c>FileNotFoundException</c> handles errors when trying to read files.
6666
</p>
6767

68-
<program language="java">
68+
<program xml:id="io-exception-imports" language="java">
6969
import java.io.IOException;
7070
import java.io.FileNotFoundException;
7171
</program>
@@ -80,16 +80,16 @@
8080
We will now create a <c>File</c> object. It is important to create a meaningful name for the <c>File</c> object. We will call ours <c>myFile</c>. We will also call our class <c>CreateFile</c>
8181
</p>
8282

83-
<program xml:id="create-file-java" interactive="activecode" language="java" add-files="myfile-created">
84-
<code>
83+
<program xml:id="create-file-class-java" interactive="activecode" language="java" add-files="myfile-created">
84+
<input>
8585
import java.io.File;
8686
public class CreateFile {
8787
public static void main(String[] args) {
8888
File myFile = new File("myfile.txt");
8989
System.out.println(myFile);
9090
}
9191
}
92-
</code>
92+
</input>
9393
</program>
9494

9595
<note>
@@ -108,7 +108,7 @@
108108
</p>
109109

110110
<program xml:id="create-file-python" interactive="activecode" language="python">
111-
<code>
111+
<input>
112112
filename = "newfile.txt"
113113
print("Attempting to write to '" + filename + "' using 'w' mode...")
114114
try:
@@ -119,15 +119,15 @@
119119
# This would only catch other unexpected errors
120120
print("An unexpected error occurred during write: " + str(e))
121121

122-
</code>
122+
</input>
123123
</program>
124124

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

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

@@ -147,7 +147,7 @@
147147
}
148148
}
149149
}
150-
</code>
150+
</input>
151151
</program>
152152

153153
<note>
@@ -179,8 +179,8 @@
179179
</pre>
180180
</datafile>
181181

182-
<program xml:id="file-read-py" interactive="activecode" language="python" add-files="myfile-read">
183-
<code>
182+
<program xml:id="file-read-python" interactive="activecode" language="python" add-files="myfile-read">
183+
<input>
184184
filename = "myfile.txt"
185185
try:
186186
# Attempt to open the file in read mode ('r')
@@ -191,15 +191,15 @@
191191
except:
192192
#catches if the file doesn't exist or can't be written to
193193
print("file could not be opened")
194-
</code>
194+
</input>
195195
</program>
196196

197197
<p>
198198
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.
199199
</p>
200200

201201
<program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read">
202-
<code>
202+
<input>
203203
import java.io.File;
204204
import java.io.FileNotFoundException;
205205
import java.util.Scanner;
@@ -217,7 +217,7 @@
217217
}
218218
}
219219
}
220-
</code>
220+
</input>
221221
</program>
222222
<p>
223223
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.
@@ -235,7 +235,7 @@
235235
Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>:
236236
</p>
237237

238-
<program language="java">
238+
<program xml:id="write-file-class-java" language="java">
239239
import java.io.File;
240240
import java.io.FileWriter;
241241
import java.io.IOException;
@@ -251,15 +251,15 @@
251251
Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>:
252252
</p>
253253

254-
<program language="java">
254+
<program xml:id="filerwriter-object-java" language="java">
255255
FileWriter myWriter = new FileWriter("myfile.txt");
256256
</program>
257257

258258
<p>
259259
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:
260260
</p>
261261

262-
<program language="java">
262+
<program xml:id="write-and-clase-java" language="java">
263263
myWriter.write("File successfully updated!");
264264
myWriter.close();
265265
</program>
@@ -280,8 +280,8 @@
280280
3
281281
</pre>
282282
</datafile>
283-
<program xml:id="file-write-exp2" interactive="activecode" language="python" add-files = "my-file-8-4-2">
284-
<code>
283+
<program xml:id="file-try-catch-python" interactive="activecode" language="python" add-files = "my-file-8-4-2">
284+
<input>
285285
try:
286286
with open("myfile.txt", "w") as my_writer:
287287
my_writer.write("File successfully updated!")
@@ -290,7 +290,7 @@
290290
print("An error occurred.")
291291
import traceback
292292
traceback.print_exc()
293-
</code>
293+
</input>
294294
</program>
295295

296296
<p>
@@ -301,7 +301,7 @@
301301

302302
</pre>
303303
</datafile>
304-
<program language="java">
304+
<program language="java" xml:id="file-try-catch-java">
305305
try {
306306
FileWriter myWriter = new FileWriter("myfile.txt");
307307
myWriter.write("File successfully updated!");
@@ -321,8 +321,8 @@
321321

322322
</pre>
323323
</datafile>
324-
<program xml:id="file-write-exp4" interactive="activecode" language="python" add-files = "my-file-8-4-4">
325-
<code>
324+
<program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4">
325+
<input>
326326
try:
327327
with open("myfile.txt", "w") as my_writer:
328328
my_writer.write("File successfully updated!")
@@ -331,7 +331,7 @@
331331
print("An error occurred.")
332332
import traceback
333333
traceback.print_exc()
334-
</code>
334+
</input>
335335
</program>
336336

337337
<p>
@@ -342,8 +342,8 @@
342342

343343
</pre>
344344
</datafile>
345-
<program xml:id="file-write-exp5" interactive="activecode" language="java" add-files = "my-file-8-4-5">
346-
<code>
345+
<program xml:id="file-write-full-java" interactive="activecode" language="java" add-files = "my-file-8-4-5">
346+
<input>
347347
import java.io.FileWriter;
348348
import java.io.IOException;
349349

@@ -360,7 +360,7 @@
360360
}
361361
}
362362
}
363-
</code>
363+
</input>
364364
</program>
365365

366366
<note>
@@ -373,20 +373,20 @@
373373
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:
374374
</p>
375375

376-
<program language="java">
376+
<program xml:id="append-true" language="java">
377377
FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode
378378
</program>
379379

380380
<p>
381381
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:
382382
</p>
383383
<datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes">
384-
<program language="java">
384+
<pre>
385385

386-
</program>
386+
</pre>
387387
</datafile>
388-
<program xml:id="file-write-exp6" interactive="activecode" language="java" add-files = "my-file-8-4-6">
389-
<code>
388+
<program xml:id="file-write-with-append" interactive="activecode" language="java" add-files = "my-file-8-4-6">
389+
<input>
390390
import java.io.FileWriter;
391391
import java.io.IOException;
392392

@@ -403,22 +403,22 @@
403403
}
404404
}
405405
}
406-
</code>
406+
</input>
407407
</program>
408408

409409
<p>
410410
Then if we run the program twice, the contents of <c>myfile.txt</c> would be:
411411
</p>
412412

413-
<program language="java">
413+
<pre>
414414
File successfully updated!File successfully updated!
415-
</program>
415+
</pre>
416416

417417
<p>
418418
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:
419419
</p>
420420

421-
<program language="java">
421+
<program xml:id="newline" language="java">
422422
myWriter.write("File successfully updated!\n"); // Added newline character
423423
myWriter.close();
424424
</program>
@@ -453,7 +453,7 @@
453453
</pre>
454454
</datafile>
455455

456-
<program interactive="activecode" language="python">
456+
<program xml:id="delete-file-python" interactive="activecode" language="python">
457457
<input>
458458
# Name of the file to delete
459459
file_name = "myfile.txt"
@@ -470,8 +470,8 @@
470470
</input>
471471
</program>
472472

473-
<program interactive="activecode" language="java">
474-
<code>
473+
<program xml:id="delete-file-java" interactive="activecode" language="java">
474+
<input>
475475
import java.io.File;
476476

477477
public class DeleteFile {
@@ -484,7 +484,7 @@
484484
}
485485
}
486486
}
487-
</code>
487+
</input>
488488
</program>
489489

490490
<p>

0 commit comments

Comments
 (0)