Skip to content

Commit bf5eed8

Browse files
committed
add listing tags
1 parent 35cc975 commit bf5eed8

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,22 @@
99
<p>
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>
12-
<program xml:id = "file-class-import-Python-example" interactive="activecode" language="python">
12+
13+
<listing xml:id = "file-class-import-Python-example">
14+
<program interactive="activecode" language="python">
1315
<code>
1416
import math
1517
print(math.sqrt(25))
1618
</code>
1719
</program>
20+
</listing>
1821

1922
<p>
2023
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:
2124
</p>
22-
<program xml:id = "file-class-import-Java-example" interactive="activecode" language="java">
25+
26+
<listing xml:id = "file-class-import-Java-example">
27+
<program interactive="activecode" language="java">
2328
<code>
2429
import java.lang.Math;
2530

@@ -30,6 +35,8 @@
3035
}
3136
</code>
3237
</program>
38+
</listing>
39+
3340
<p>
3441
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.
3542
</p>
@@ -38,34 +45,42 @@
3845
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.
3946
</p>
4047

41-
<program xml:id="io-file-import" language="java">
48+
<listing xml:id = "io-file-import">
49+
<program language="java">
4250
import java.io.File;
4351
</program>
52+
</listing>
53+
4454
<p>
4555
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.
4656
</p>
47-
48-
<program xml:id="util-scanner-import" language="java">
57+
<listing xml:id = "util-scanner-import">
58+
<program language="java">
4959
import java.util.Scanner;
5060
</program>
61+
</listing>
5162

5263
<p>
5364
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.
5465
</p>
5566

56-
<program xml:id="io-filewriter-import" language="java">
67+
<listing xml:id = "io-filewriter-import">
68+
<program language="java">
5769
import java.io.FileWriter;
5870
</program>
71+
</listing>
5972

6073
<p>
6174
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.
6275
</p>
6376

64-
<program xml:id="io-exception-imports" language="java">
77+
<listing xml:id = "io-exception-imports">
78+
<program language="java">
6579
import java.io.IOException;
6680
import java.io.FileNotFoundException;
6781
</program>
68-
82+
</listing>
83+
6984
</section>
7085

7186
<section xml:id="creating-files">

0 commit comments

Comments
 (0)