|
446 | 446 | <title>Deleting Files</title> |
447 | 447 |
|
448 | 448 | <p> |
449 | | - 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. |
450 | | - </p> |
451 | | - |
452 | | - <datafile label="myfile-delete" filename="myfile.txt" xml:id= "myfile-delete" editable="no"> |
453 | | - <pre> |
454 | | - 1 |
455 | | - 2 |
456 | | - 3 |
457 | | - 4 |
458 | | - 5 |
459 | | - 6 |
460 | | - 7 |
461 | | - 8 |
462 | | - </pre> |
463 | | - </datafile> |
464 | | - |
465 | | - <program xml:id="delete-file-python" interactive="activecode" language="python"> |
| 449 | + 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. First, the CreateFile class from before will be used to create a file: |
| 450 | + </p> |
| 451 | + |
| 452 | + <program xml:id="create-file-to-delete-java" interactive="activecode" language="java"> |
466 | 453 | <input> |
467 | | - # Name of the file to delete |
468 | | - file_name = "myfile.txt" |
469 | | - |
470 | | - # Check if the file exists before deleting |
471 | | - if os.path.exists(file_name): |
472 | | - try: |
473 | | - os.remove(file_name) |
474 | | - print("Deleted", file_name) |
475 | | - except Exception as e: |
476 | | - print("File could not be deleted.") |
477 | | - else: |
478 | | - print("File could not be deleted.") |
479 | | - </input> |
| 454 | + import java.io.File; |
| 455 | + import java.io.IOException; |
| 456 | + |
| 457 | + public class CreateFile { |
| 458 | + public static void main(String[] args) { |
| 459 | + File myFile = new File("myfile.txt"); |
| 460 | + try { |
| 461 | + if (myFile.createNewFile()) { |
| 462 | + System.out.println("The file " + myFile.getName() + " was created successfully."); |
| 463 | + } else { |
| 464 | + System.out.println("The file " + myFile.getName() + " already exists."); |
| 465 | + } |
| 466 | + } catch (IOException e) { |
| 467 | + // This code runs if an IOException occurs |
| 468 | + System.out.println("An error occurred while creating the file."); |
| 469 | + e.printStackTrace(); // This prints the stack trace for more detailed error info |
| 470 | + } |
| 471 | + } |
| 472 | + } |
| 473 | + </input> |
480 | 474 | </program> |
481 | 475 |
|
| 476 | + <p> |
| 477 | + The next example is Python code that can be used to delete files. This code cannot be run because importing <c>os</c> is not possible with the technologies used to write this book: |
| 478 | + </p> |
| 479 | + |
| 480 | + <program xml:id="delete-file-java" language="python"> |
| 481 | + import os |
| 482 | + file_name = "myfile.txt" |
| 483 | + if os.path.exists(file_name): |
| 484 | + try: |
| 485 | + os.remove(file_name) |
| 486 | + print("Deleted", file_name) |
| 487 | + except Exception as e: |
| 488 | + print("File could not be deleted.") |
| 489 | + else: |
| 490 | + print("File could not be deleted.") |
| 491 | + </program> |
| 492 | + |
| 493 | + <p> |
| 494 | + And finally, we have Java code that deletes files. We will call this class DeleteFile: |
| 495 | + </p> |
| 496 | + |
| 497 | + |
482 | 498 | <program xml:id="delete-file-java" interactive="activecode" language="java"> |
483 | 499 | <input> |
484 | 500 | import java.io.File; |
|
0 commit comments