|
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 | | - <pre> |
| 45 | + <program language="java"> |
46 | 46 | import java.io.File; |
47 | | - </pre> |
| 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 | | - <pre> |
| 52 | + <program language="java"> |
53 | 53 | import java.util.Scanner; |
54 | | - </pre> |
| 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 | | - <pre> |
| 60 | + <program language="java"> |
61 | 61 | import java.io.FileWriter; |
62 | | - </pre> |
| 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 | | - <pre> |
| 68 | + <program language="java"> |
69 | 69 | import java.io.IOException; |
70 | 70 | import java.io.FileNotFoundException; |
71 | | - </pre> |
| 71 | + </program> |
72 | 72 |
|
73 | 73 | </section> |
74 | 74 |
|
|
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 | | - <pre> |
| 238 | + <program language="java"> |
239 | 239 | import java.io.File; |
240 | 240 | import java.io.FileWriter; |
241 | 241 | import java.io.IOException; |
|
245 | 245 |
|
246 | 246 | } |
247 | 247 | } |
248 | | - </pre> |
| 248 | + </program> |
249 | 249 |
|
250 | 250 | <p> |
251 | 251 | Next, we will create a <c>FileWriter</c> object. Let's call it <c>myWriter</c>: |
252 | 252 | </p> |
253 | 253 |
|
254 | | - <pre> |
| 254 | + <program language="java"> |
255 | 255 | FileWriter myWriter = new FileWriter("myfile.txt"); |
256 | | - </pre> |
| 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 | | - <pre> |
| 262 | + <program language="java"> |
263 | 263 | myWriter.write("File successfully updated!"); |
264 | 264 | myWriter.close(); |
265 | | - </pre> |
| 265 | + </program> |
266 | 266 |
|
267 | 267 | <note> |
268 | 268 | <p> |
|
301 | 301 |
|
302 | 302 | </pre> |
303 | 303 | </datafile> |
304 | | - <pre> |
| 304 | + <program language="java"> |
305 | 305 | try { |
306 | 306 | FileWriter myWriter = new FileWriter("myfile.txt"); |
307 | 307 | myWriter.write("File successfully updated!"); |
|
311 | 311 | System.out.println("An error occurred."); |
312 | 312 | e.printStackTrace(); |
313 | 313 | } |
314 | | - </pre> |
| 314 | + </program> |
315 | 315 |
|
316 | 316 | <p> |
317 | 317 | And that's it! We will add our code to the foundational code for a complete program. First, an example of equivalent Python code: |
|
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 | | - <pre> |
| 376 | + <program language="java"> |
377 | 377 | FileWriter myWriter = new FileWriter("myfile.txt", true); // true enables append mode |
378 | | - </pre> |
| 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 | | - <pre> |
| 384 | + <program language="java"> |
385 | 385 |
|
386 | | - </pre> |
| 386 | + </program> |
387 | 387 | </datafile> |
388 | 388 | <program xml:id="file-write-exp6" interactive="activecode" language="java" add-files = "my-file-8-4-6"> |
389 | 389 | <code> |
|
410 | 410 | Then if we run the program twice, the contents of <c>myfile.txt</c> would be: |
411 | 411 | </p> |
412 | 412 |
|
413 | | - <pre> |
| 413 | + <program language="java"> |
414 | 414 | File successfully updated!File successfully updated! |
415 | | - </pre> |
| 415 | + </program> |
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 | | - <pre> |
| 421 | + <program language="java"> |
422 | 422 | myWriter.write("File successfully updated!\n"); // Added newline character |
423 | 423 | myWriter.close(); |
424 | | - </pre> |
| 424 | + </program> |
425 | 425 |
|
426 | 426 | <p> |
427 | 427 | Running the code with the newline character twice will result in the following contents in myfile.txt: |
|
439 | 439 | <p> |
440 | 440 | Finally, we will take a look at using Java to delete files. This one is pretty straight-forward and follows the structure used to create files. This time, however, try/catch blocks are not needed for the program to compile. We will call this class DeleteFile. The completed code should look something like this. |
441 | 441 | </p> |
| 442 | + |
| 443 | + <datafile label="myfile-delete" filename="myfile.txt" xml:id= "myfile-delete" editable="no"> |
| 444 | + <pre> |
| 445 | + 1 |
| 446 | + 2 |
| 447 | + 3 |
| 448 | + 4 |
| 449 | + 5 |
| 450 | + 6 |
| 451 | + 7 |
| 452 | + 8 |
| 453 | + </pre> |
| 454 | + </datafile> |
| 455 | + |
| 456 | + <program interactive="activecode" language="python"> |
| 457 | + <input> |
| 458 | + # Name of the file to delete |
| 459 | + file_name = "myfile.txt" |
| 460 | + |
| 461 | + # Check if the file exists before deleting |
| 462 | + if os.path.exists(file_name): |
| 463 | + try: |
| 464 | + os.remove(file_name) |
| 465 | + print("Deleted", file_name) |
| 466 | + except Exception as e: |
| 467 | + print("File could not be deleted.") |
| 468 | + else: |
| 469 | + print("File could not be deleted.") |
| 470 | + </input> |
| 471 | + </program> |
442 | 472 |
|
443 | 473 | <program interactive="activecode" language="java"> |
444 | 474 | <code> |
|
0 commit comments