Skip to content

Commit a27432c

Browse files
authored
Merge pull request #345 from habibasorour/issue_330_listing
Issue 330: 8.1 listing
2 parents 363513f + b51dc8d commit a27432c

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
<section xml:id="class-imports">
88
<title>Class Imports</title>
99
<p>
10-
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.
10+
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. <xref ref="file-class-import-Python-example" text="type-global"/> shows an example of importing the <c>math</c> library in Python.
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>
20-
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:
23+
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 <xref ref="file-class-import-Java-example" text="type-global"/>.
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,42 +35,52 @@
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>
3643

3744
<p>
38-
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.
45+
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 shown in <xref ref="io-file-import" text="type-global"/>. 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>
4250
import java.io.File;
4351
</program>
52+
</listing>
53+
4454
<p>
45-
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.
55+
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 as shown in <xref ref="util-scanner-import" text="type-global"/>. 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>
4959
import java.util.Scanner;
5060
</program>
61+
</listing>
5162

5263
<p>
53-
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.
64+
The <c>FileWriter</c> class can be used to write to files as shown in <xref ref="io-filewriter-import" text="type-global"/>. 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>
5769
import java.io.FileWriter;
5870
</program>
71+
</listing>
5972

6073
<p>
61-
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.
74+
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 as shown in <xref ref="io-exception-imports" text="type-global"/>. <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>
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)