|
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 | 12 |
|
13 | 13 | <listing xml:id = "file-class-import-Python-example"> |
|
20 | 20 | </listing> |
21 | 21 |
|
22 | 22 | <p> |
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 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"/>. |
24 | 24 | </p> |
25 | 25 |
|
26 | | - <listing xml:id = "file-class-import-Java-example"> |
| 26 | + <listing xml:id ="file-class-import-Java-example"> |
27 | 27 | <program interactive="activecode" language="java"> |
28 | 28 | <code> |
29 | 29 | import java.lang.Math; |
|
42 | 42 | </p> |
43 | 43 |
|
44 | 44 | <p> |
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. 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. |
46 | 46 | </p> |
47 | 47 |
|
48 | 48 | <listing xml:id = "io-file-import"> |
|
52 | 52 | </listing> |
53 | 53 |
|
54 | 54 | <p> |
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. 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. |
56 | 56 | </p> |
57 | 57 | <listing xml:id = "util-scanner-import"> |
58 | 58 | <program> |
|
61 | 61 | </listing> |
62 | 62 |
|
63 | 63 | <p> |
64 | | - 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. |
65 | 65 | </p> |
66 | 66 |
|
67 | 67 | <listing xml:id = "io-filewriter-import"> |
|
71 | 71 | </listing> |
72 | 72 |
|
73 | 73 | <p> |
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. <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. |
75 | 75 | </p> |
76 | 76 |
|
77 | 77 | <listing xml:id = "io-exception-imports"> |
|
0 commit comments