|
10 | 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 | | - <code> |
| 13 | + <input> |
14 | 14 | import math |
15 | 15 | print(math.sqrt(25)) |
16 | | - </code> |
| 16 | + </input> |
17 | 17 | </program> |
18 | 18 |
|
19 | 19 | <p> |
20 | 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 | | - <code> |
| 23 | + <input> |
24 | 24 | import java.lang.Math; |
25 | 25 |
|
26 | 26 | public class SquareRoot { |
27 | 27 | public static void main(String[] args) { |
28 | 28 | System.out.println(Math.sqrt(25)); |
29 | 29 | } |
30 | 30 | } |
31 | | - </code> |
| 31 | + </input> |
32 | 32 | </program> |
33 | 33 | <p> |
34 | 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. |
|
42 | 42 | 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. |
43 | 43 | </p> |
44 | 44 |
|
45 | | - <program language="java"> |
| 45 | + <program xml:id="io-file-import" language="java"> |
46 | 46 | import java.io.File; |
47 | 47 | </program> |
48 | 48 | <p> |
49 | 49 | 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. |
50 | 50 | </p> |
51 | 51 |
|
52 | | - <program language="java"> |
| 52 | + <program xml:id="util-scanner-import" language="java"> |
53 | 53 | import java.util.Scanner; |
54 | 54 | </program> |
55 | 55 |
|
56 | 56 | <p> |
57 | 57 | 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. |
58 | 58 | </p> |
59 | 59 |
|
60 | | - <program language="java"> |
| 60 | + <program xml:id="io-filewriter-import" language="java"> |
61 | 61 | import java.io.FileWriter; |
62 | 62 | </program> |
63 | 63 |
|
64 | 64 | <p> |
65 | 65 | 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. |
66 | 66 | </p> |
67 | 67 |
|
68 | | - <program language="java"> |
| 68 | + <program xml:id="io-exception-imports" language="java"> |
69 | 69 | import java.io.IOException; |
70 | 70 | import java.io.FileNotFoundException; |
71 | 71 | </program> |
|
80 | 80 | 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>. We will also call our class <c>CreateFile</c> |
81 | 81 | </p> |
82 | 82 |
|
83 | | - <program xml:id="create-file-java" interactive="activecode" language="java" add-files="myfile-created"> |
84 | | - <code> |
| 83 | + <program xml:id="create-file-class-java" interactive="activecode" language="java" add-files="myfile-created"> |
| 84 | + <input> |
85 | 85 | import java.io.File; |
86 | 86 | public class CreateFile { |
87 | 87 | public static void main(String[] args) { |
88 | 88 | File myFile = new File("myfile.txt"); |
89 | 89 | System.out.println(myFile); |
90 | 90 | } |
91 | 91 | } |
92 | | - </code> |
| 92 | + </input> |
93 | 93 | </program> |
94 | 94 |
|
95 | 95 | <note> |
|
108 | 108 | </p> |
109 | 109 |
|
110 | 110 | <program xml:id="create-file-python" interactive="activecode" language="python"> |
111 | | - <code> |
| 111 | + <input> |
112 | 112 | filename = "newfile.txt" |
113 | 113 | print("Attempting to write to '" + filename + "' using 'w' mode...") |
114 | 114 | try: |
|
119 | 119 | # This would only catch other unexpected errors |
120 | 120 | print("An unexpected error occurred during write: " + str(e)) |
121 | 121 |
|
122 | | - </code> |
| 122 | + </input> |
123 | 123 | </program> |
124 | 124 |
|
125 | 125 | <p> |
126 | 126 | Now, let's look at Java code that accomplishes the same task: |
127 | 127 | </p> |
128 | 128 |
|
129 | | - <program xml:id="create-file-exp3" interactive="activecode" language="java"> |
130 | | - <code> |
| 129 | + <program xml:id="create-file-java" interactive="activecode" language="java"> |
| 130 | + <input> |
131 | 131 | import java.io.File; |
132 | 132 | import java.io.IOException; |
133 | 133 |
|
|
147 | 147 | } |
148 | 148 | } |
149 | 149 | } |
150 | | - </code> |
| 150 | + </input> |
151 | 151 | </program> |
152 | 152 |
|
153 | 153 | <note> |
|
179 | 179 | </pre> |
180 | 180 | </datafile> |
181 | 181 |
|
182 | | - <program xml:id="file-read-py" interactive="activecode" language="python" add-files="myfile-read"> |
183 | | - <code> |
| 182 | + <program xml:id="file-read-python" interactive="activecode" language="python" add-files="myfile-read"> |
| 183 | + <input> |
184 | 184 | filename = "myfile.txt" |
185 | 185 | try: |
186 | 186 | # Attempt to open the file in read mode ('r') |
|
191 | 191 | except: |
192 | 192 | #catches if the file doesn't exist or can't be written to |
193 | 193 | print("file could not be opened") |
194 | | - </code> |
| 194 | + </input> |
195 | 195 | </program> |
196 | 196 |
|
197 | 197 | <p> |
198 | 198 | 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. |
199 | 199 | </p> |
200 | 200 |
|
201 | 201 | <program xml:id="file-read-java" interactive="activecode" language="java" add-files= "myfile-read"> |
202 | | - <code> |
| 202 | + <input> |
203 | 203 | import java.io.File; |
204 | 204 | import java.io.FileNotFoundException; |
205 | 205 | import java.util.Scanner; |
|
217 | 217 | } |
218 | 218 | } |
219 | 219 | } |
220 | | - </code> |
| 220 | + </input> |
221 | 221 | </program> |
222 | 222 | <p> |
223 | 223 | 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 | Let us create the framework for a class that will write to a file. Let's call this class <c>WriteFile</c>: |
236 | 236 | </p> |
237 | 237 |
|
238 | | - <program language="java"> |
| 238 | + <program xml:id="write-file-class-java" language="java"> |
239 | 239 | import java.io.File; |
240 | 240 | import java.io.FileWriter; |
241 | 241 | import java.io.IOException; |
|
251 | 251 | Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>: |
252 | 252 | </p> |
253 | 253 |
|
254 | | - <program language="java"> |
| 254 | + <program xml:id="filerwriter-object-java" language="java"> |
255 | 255 | FileWriter myWriter = new FileWriter("myfile.txt"); |
256 | 256 | </program> |
257 | 257 |
|
258 | 258 | <p> |
259 | 259 | In this next step, we will use the <c>write()</c> method from the FileWriter class. This Method will take any data within the parenthesis and write that data to the file selected. The <c>write()</c> method takes most standard data types: |
260 | 260 | </p> |
261 | 261 |
|
262 | | - <program language="java"> |
| 262 | + <program xml:id="write-and-clase-java" language="java"> |
263 | 263 | myWriter.write("File successfully updated!"); |
264 | 264 | myWriter.close(); |
265 | 265 | </program> |
|
280 | 280 | 3 |
281 | 281 | </pre> |
282 | 282 | </datafile> |
283 | | - <program xml:id="file-write-exp2" interactive="activecode" language="python" add-files = "my-file-8-4-2"> |
284 | | - <code> |
| 283 | + <program xml:id="file-try-catch-python" interactive="activecode" language="python" add-files = "my-file-8-4-2"> |
| 284 | + <input> |
285 | 285 | try: |
286 | 286 | with open("myfile.txt", "w") as my_writer: |
287 | 287 | my_writer.write("File successfully updated!") |
|
290 | 290 | print("An error occurred.") |
291 | 291 | import traceback |
292 | 292 | traceback.print_exc() |
293 | | - </code> |
| 293 | + </input> |
294 | 294 | </program> |
295 | 295 |
|
296 | 296 | <p> |
|
301 | 301 |
|
302 | 302 | </pre> |
303 | 303 | </datafile> |
304 | | - <program language="java"> |
| 304 | + <program language="java" xml:id="file-try-catch-java"> |
305 | 305 | try { |
306 | 306 | FileWriter myWriter = new FileWriter("myfile.txt"); |
307 | 307 | myWriter.write("File successfully updated!"); |
|
321 | 321 |
|
322 | 322 | </pre> |
323 | 323 | </datafile> |
324 | | - <program xml:id="file-write-exp4" interactive="activecode" language="python" add-files = "my-file-8-4-4"> |
325 | | - <code> |
| 324 | + <program xml:id="file-write-full-python" interactive="activecode" language="python" add-files="my-file-8-4-4"> |
| 325 | + <input> |
326 | 326 | try: |
327 | 327 | with open("myfile.txt", "w") as my_writer: |
328 | 328 | my_writer.write("File successfully updated!") |
|
331 | 331 | print("An error occurred.") |
332 | 332 | import traceback |
333 | 333 | traceback.print_exc() |
334 | | - </code> |
| 334 | + </input> |
335 | 335 | </program> |
336 | 336 |
|
337 | 337 | <p> |
|
342 | 342 |
|
343 | 343 | </pre> |
344 | 344 | </datafile> |
345 | | - <program xml:id="file-write-exp5" interactive="activecode" language="java" add-files = "my-file-8-4-5"> |
346 | | - <code> |
| 345 | + <program xml:id="file-write-full-java" interactive="activecode" language="java" add-files = "my-file-8-4-5"> |
| 346 | + <input> |
347 | 347 | import java.io.FileWriter; |
348 | 348 | import java.io.IOException; |
349 | 349 |
|
|
360 | 360 | } |
361 | 361 | } |
362 | 362 | } |
363 | | - </code> |
| 363 | + </input> |
364 | 364 | </program> |
365 | 365 |
|
366 | 366 | <note> |
|
373 | 373 | Speaking of overwriting data, what if we want to append text to the end of any text already in <c>myfile.txt</c>? To accomplish this, we can pass a <c>boolean</c> argument along with the file name when creating a new data argument: |
374 | 374 | </p> |
375 | 375 |
|
376 | | - <program language="java"> |
| 376 | + <program xml:id="append-true" language="java"> |
377 | 377 | FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode |
378 | 378 | </program> |
379 | 379 |
|
380 | 380 | <p> |
381 | 381 | Now, when we use <c>write()</c> method like before, the text will be appended if there is already tSext in the document. If we were to update our code to include the <c>boolean</c> argument: |
382 | 382 | </p> |
383 | 383 | <datafile label="my-file-8-4-6" filename="myfile.txt" xml:id= "my-file-8-4-6" editable="yes"> |
384 | | - <program language="java"> |
| 384 | + <pre> |
385 | 385 |
|
386 | | - </program> |
| 386 | + </pre> |
387 | 387 | </datafile> |
388 | | - <program xml:id="file-write-exp6" interactive="activecode" language="java" add-files = "my-file-8-4-6"> |
389 | | - <code> |
| 388 | + <program xml:id="file-write-with-append" interactive="activecode" language="java" add-files = "my-file-8-4-6"> |
| 389 | + <input> |
390 | 390 | import java.io.FileWriter; |
391 | 391 | import java.io.IOException; |
392 | 392 |
|
|
403 | 403 | } |
404 | 404 | } |
405 | 405 | } |
406 | | - </code> |
| 406 | + </input> |
407 | 407 | </program> |
408 | 408 |
|
409 | 409 | <p> |
410 | 410 | Then if we run the program twice, the contents of <c>myfile.txt</c> would be: |
411 | 411 | </p> |
412 | 412 |
|
413 | | - <program language="java"> |
| 413 | + <pre> |
414 | 414 | File successfully updated!File successfully updated! |
415 | | - </program> |
| 415 | + </pre> |
416 | 416 |
|
417 | 417 | <p> |
418 | 418 | This doesn't look very good! If we want each additional write to appear on a new line? A simple solution is to use the <c>\n</c> newline character: |
419 | 419 | </p> |
420 | 420 |
|
421 | | - <program language="java"> |
| 421 | + <program xml:id="newline" language="java"> |
422 | 422 | myWriter.write("File successfully updated!\n"); // Added newline character |
423 | 423 | myWriter.close(); |
424 | 424 | </program> |
|
453 | 453 | </pre> |
454 | 454 | </datafile> |
455 | 455 |
|
456 | | - <program interactive="activecode" language="python"> |
| 456 | + <program xml:id="delete-file-python" interactive="activecode" language="python"> |
457 | 457 | <input> |
458 | 458 | # Name of the file to delete |
459 | 459 | file_name = "myfile.txt" |
|
470 | 470 | </input> |
471 | 471 | </program> |
472 | 472 |
|
473 | | - <program interactive="activecode" language="java"> |
474 | | - <code> |
| 473 | + <program xml:id="delete-file-java" interactive="activecode" language="java"> |
| 474 | + <input> |
475 | 475 | import java.io.File; |
476 | 476 |
|
477 | 477 | public class DeleteFile { |
|
484 | 484 | } |
485 | 485 | } |
486 | 486 | } |
487 | | - </code> |
| 487 | + </input> |
488 | 488 | </program> |
489 | 489 |
|
490 | 490 | <p> |
|
0 commit comments