|
7 | 7 | <section xml:id="class-imports"> |
8 | 8 | <title>Class Imports</title> |
9 | 9 | <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. |
11 | 11 | </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"> |
13 | 15 | <code> |
14 | 16 | import math |
15 | 17 | print(math.sqrt(25)) |
16 | 18 | </code> |
17 | 19 | </program> |
| 20 | + </listing> |
18 | 21 |
|
19 | 22 | <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"/>. |
21 | 24 | </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"> |
23 | 28 | <code> |
24 | 29 | import java.lang.Math; |
25 | 30 |
|
|
30 | 35 | } |
31 | 36 | </code> |
32 | 37 | </program> |
| 38 | + </listing> |
| 39 | + |
33 | 40 | <p> |
34 | 41 | 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. |
35 | 42 | </p> |
36 | 43 |
|
37 | 44 | <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. |
39 | 46 | </p> |
40 | 47 |
|
41 | | - <program xml:id="io-file-import" language="java"> |
| 48 | + <listing xml:id = "io-file-import"> |
| 49 | + <program> |
42 | 50 | import java.io.File; |
43 | 51 | </program> |
| 52 | + </listing> |
| 53 | + |
44 | 54 | <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. |
46 | 56 | </p> |
47 | | - |
48 | | - <program xml:id="util-scanner-import" language="java"> |
| 57 | + <listing xml:id = "util-scanner-import"> |
| 58 | + <program> |
49 | 59 | import java.util.Scanner; |
50 | 60 | </program> |
| 61 | + </listing> |
51 | 62 |
|
52 | 63 | <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. |
54 | 65 | </p> |
55 | 66 |
|
56 | | - <program xml:id="io-filewriter-import" language="java"> |
| 67 | + <listing xml:id = "io-filewriter-import"> |
| 68 | + <program> |
57 | 69 | import java.io.FileWriter; |
58 | 70 | </program> |
| 71 | + </listing> |
59 | 72 |
|
60 | 73 | <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. |
62 | 75 | </p> |
63 | 76 |
|
64 | | - <program xml:id="io-exception-imports" language="java"> |
| 77 | + <listing xml:id = "io-exception-imports"> |
| 78 | + <program> |
65 | 79 | import java.io.IOException; |
66 | 80 | import java.io.FileNotFoundException; |
67 | 81 | </program> |
68 | | - |
| 82 | + </listing> |
| 83 | + |
69 | 84 | </section> |
70 | 85 |
|
71 | 86 | <section xml:id="creating-files"> |
|
0 commit comments