|
1 | 1 | <?xml version="1.0"?> |
2 | 2 |
|
3 | 3 | <!-- Generated by Docutils 0.19 --> |
4 | | -<chapter xml:id="filemmanipulation"> |
| 4 | +<chapter xml:id="file-handling"> |
5 | 5 | <title>File Handling</title> |
6 | 6 |
|
7 | | - <section xml:id="file-class-import"> |
| 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 read from, write to, create, delete, move, and copy 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. Consider the following. |
11 | 11 | </p> |
12 | 12 | <program xml:id = "file-class-import-Python-example" interactive="activecode" language="python"> |
13 | 13 | <code> |
|
17 | 17 | </program> |
18 | 18 |
|
19 | 19 | <p> |
20 | | - The same program in Java would look like this: |
| 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: |
21 | 21 | </p> |
22 | 22 | <program xml:id = "file-class-import-Java-example" interactive="activecode" language="java"> |
23 | 23 | <code> |
|
31 | 31 | </code> |
32 | 32 | </program> |
33 | 33 | <p> |
34 | | - Note the use of <c>import java.lang.Math;</c> in the above to import the Math class. Unlike Python, Java requires explicit imports for most libraries, including the <c>Math</c> class and many different classes for file handling. |
| 34 | + 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 | 35 | </p> |
36 | 36 |
|
37 | 37 | <p> |
38 | | - Much like the Math class, in order for your program to work with files you need to import classes from libraries. Java includes a class called <c>File</c> in the <c>io</c> library. This class allows you to create File objects, and use its public methods. the following code imports the <c>File</c> class and creates a <c>File</c> object called myFile. for now focus on how the class is imported and used in the program; We will cover the <c>IOException</c> class and <c>createNewFile</c> method later. |
| 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. the following code imports the <c>File</c> class and creates a <c>File</c> object called myFile. for now focus on how the class is imported and used in the program; We will cover the <c>IOException</c> class and <c>createNewFile</c> method later. |
39 | 39 | </p> |
40 | 40 | <program xml:id = "file-class-import-io-example" interactive="activecode" language="java"> |
41 | 41 | <code> |
|
82 | 82 |
|
83 | 83 | </section> |
84 | 84 |
|
85 | | - <section xml:id="create-file"> |
| 85 | + <section xml:id="creating-files"> |
86 | 86 | <title>Creating Files</title> |
87 | 87 |
|
88 | 88 | <p> |
89 | 89 | 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>. |
90 | 90 | </p> |
91 | | - <datafile label="my-file-8-2-1" filename="myfile.txt" xml:id= "my-file-8-2-1" editable="yes"> |
| 91 | + <datafile label="myfile-created" filename="myfile.txt" xml:id= "myfile-created" editable="yes"> |
92 | 92 | <pre> |
93 | 93 | empty file |
94 | 94 | </pre> |
95 | 95 | </datafile> |
96 | | - <program xml:id="create-file-exp-1" interactive="activecode" language="java" add-files="my-file-8-2-1"> |
| 96 | + <program xml:id="create-file-java" interactive="activecode" language="java" add-files="myfile-created"> |
97 | 97 | <code> |
98 | 98 | import java.io.File; |
99 | 99 | public class Main { |
|
119 | 119 | First, lets look at the equivalent Python code: |
120 | 120 | </p> |
121 | 121 |
|
122 | | - <program xml:id="creat-file-exp-2" interactive="activecode" language="python"> |
| 122 | + <program xml:id="create-file-python" interactive="activecode" language="python"> |
123 | 123 | <code> |
124 | 124 | filename = "newfile.txt" |
125 | 125 | print("Attempting to write to '" + filename + "' using 'w' mode...") |
|
169 | 169 | </note> |
170 | 170 | </section> |
171 | 171 |
|
172 | | - <section xml:id="file-read"> |
| 172 | + <section xml:id="reading-files"> |
173 | 173 | <title>Reading Files</title> |
174 | 174 |
|
175 | 175 | <p> |
176 | | - Let’s take a look at how we can use Python to understand how read file contents in Java.In order to read files generally you iterate through each line in the file and read its content. In Java you read files in a very similar way, however in java we will use a <c>Scanner</c> object in order to iterate through lines. |
| 176 | + Let’s take a look at how we can use Python to understand how read file contents in Java. In order to read files generally you iterate through each line in the file and read the line's content. In Java, you read files in a very similar way, however in Java we will use the <c>Scanner</c> class in order to iterate through the lines. |
177 | 177 | </p> |
178 | 178 | <p> |
179 | | - The next lines consists of a Python code example that reads each line of the file and prints it to the console. |
| 179 | + Consider the following Python code example that reads each line of the file and prints it to the console. |
180 | 180 | </p> |
181 | | - <datafile label="my-file-8-3" filename="myfile.txt" xml:id= "my-file-8-3" editable="no"> |
| 181 | + <datafile label="myfile-read" filename="myfile.txt" xml:id= "myfile-read" editable="no"> |
182 | 182 | <pre> |
183 | 183 | 1 |
184 | 184 | 2 |
|
191 | 191 | </pre> |
192 | 192 | </datafile> |
193 | 193 |
|
194 | | - <program xml:id="file-read-exp1" interactive="activecode" language="python" add-files="my-file-8-3"> |
| 194 | + <program xml:id="file-read-py" interactive="activecode" language="python" add-files="myfile-read"> |
195 | 195 | <code> |
196 | 196 | filename = "myfile.txt" |
197 | 197 | try: |
|
210 | 210 | 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. |
211 | 211 | </p> |
212 | 212 |
|
213 | | - <program xml:id="file-read-exp2" interactive="activecode" language="java" add-files= "my-file-8-3"> |
| 213 | + <program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read"> |
214 | 214 | <code> |
215 | 215 | import java.io.File; |
216 | 216 | import java.io.FileNotFoundException; |
|
231 | 231 | </code> |
232 | 232 | </program> |
233 | 233 | <p> |
234 | | - 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 false 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. |
| 234 | + 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 | 235 | </p> |
236 | 236 | </section> |
237 | 237 |
|
238 | | - <section xml:id="file-write"> |
| 238 | + <section xml:id="writing-to-files"> |
239 | 239 | <title>Writing to Files</title> |
240 | 240 |
|
241 | 241 | <p> |
|
0 commit comments