From dc2ce86a9823cd54fa67a1cd53ca88b25022ac1b Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 08:59:09 -0400 Subject: [PATCH 1/2] improve code and clarity of Deleting Files section --- source/ch8_filehandling.ptx | 54 +++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index 63621ae..47e6171 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -440,7 +440,7 @@ public class CreateFile { Deleting Files

- 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: + Lastly, we will take a look at using Java to delete a file. This is pretty straight-forward and follows the structure used to create files. Here is the CreateFile class from before that will be used to create a file that we will soon delete:

@@ -467,47 +467,41 @@ public class CreateFile { -

- The next example is Python code that can be used to delete files. This code cannot be run because importing os is not possible with the technologies used to write this book: -

- - - import os - file_name = "myfile.txt" - if os.path.exists(file_name): - try: - os.remove(file_name) - print("Deleted", file_name) - except Exception as e: - print("File could not be deleted.") - else: - print("File could not be deleted.") -

- And finally, we have Java code that deletes files. We will call this class DeleteFile: + And finally, we have Java code that deletes a file. We will call this class DeleteFile:

- import java.io.File; - - public class DeleteFile { - public static void main(String[] args) { - File myFile = new File("myfile.txt"); - if (myFile.delete()) { - System.out.println("Deleted " + myFile.getName()); - } else { - System.out.println("File could not be deleted."); - } - } +import java.io.File; +import java.io.IOException; + +public class DeleteFile { + public static void main(String[] args) { + try { + File myFile = new File("myfile.txt"); + + // Create the file (does nothing if it already exists) + myFile.createNewFile(); + System.out.println("File created: " + myFile.getName()); + + // Delete the file + if (myFile.delete()) { + System.out.println("Deleted " + myFile.getName()); } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +

- This is almost identical to the code within the try block of the CreateFile class we made earlier. The main difference is the use of the delete() method. This method will delete any file with the name provided when creating the myFile object. Similar to the createNewFile() method, it will return true if the file existed and could be deleted, and false if the file could not be deleted. + Note that this is almost identical to the code within the try block of the CreateFile class we made earlier. The key difference is the use of the delete() method. This method will delete any file with the name that was linked to the myFile object. Similar to the createNewFile() method, it will return true if the file exists and can be deleted, and false if the file cannot be deleted.

From ba444bc3786926253c4b6c20645caf64cf471ebb Mon Sep 17 00:00:00 2001 From: Jan Pearce Date: Tue, 26 Aug 2025 09:01:58 -0400 Subject: [PATCH 2/2] improve language --- source/ch8_filehandling.ptx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/ch8_filehandling.ptx b/source/ch8_filehandling.ptx index 47e6171..89909cb 100644 --- a/source/ch8_filehandling.ptx +++ b/source/ch8_filehandling.ptx @@ -501,7 +501,7 @@ public class DeleteFile {

- Note that this is almost identical to the code within the try block of the CreateFile class we made earlier. The key difference is the use of the delete() method. This method will delete any file with the name that was linked to the myFile object. Similar to the createNewFile() method, it will return true if the file exists and can be deleted, and false if the file cannot be deleted. + Note that this is almost identical to the code within the try block of the CreateFile class that we made earlier. The key difference is the use of the delete() method which will delete the file with the name that was linked to the myFile object. Similar to the createNewFile() method, it will return true if the file exists and can be deleted, and false if the file cannot be deleted.