Skip to content

Commit dc2ce86

Browse files
committed
improve code and clarity of Deleting Files section
1 parent 1bbfacc commit dc2ce86

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

source/ch8_filehandling.ptx

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public class CreateFile {
440440
<title>Deleting Files</title>
441441

442442
<p>
443-
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:
443+
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 <c>CreateFile</c> class from before that will be used to create a file that we will soon delete:
444444
</p>
445445

446446
<program xml:id="create-file-to-delete-java" interactive="activecode" language="java">
@@ -467,47 +467,41 @@ public class CreateFile {
467467
</code>
468468
</program>
469469

470-
<p>
471-
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:
472-
</p>
473-
474-
<program xml:id="delete-file-python" language="python">
475-
import os
476-
file_name = "myfile.txt"
477-
if os.path.exists(file_name):
478-
try:
479-
os.remove(file_name)
480-
print("Deleted", file_name)
481-
except Exception as e:
482-
print("File could not be deleted.")
483-
else:
484-
print("File could not be deleted.")
485-
</program>
486470

487471
<p>
488-
And finally, we have Java code that deletes files. We will call this class DeleteFile:
472+
And finally, we have Java code that deletes a file. We will call this class <c>DeleteFile</c>:
489473
</p>
490474

491475

492476
<program xml:id="delete-file-java" interactive="activecode" language="java">
493477
<code>
494-
import java.io.File;
495-
496-
public class DeleteFile {
497-
public static void main(String[] args) {
498-
File myFile = new File("myfile.txt");
499-
if (myFile.delete()) {
500-
System.out.println("Deleted " + myFile.getName());
501-
} else {
502-
System.out.println("File could not be deleted.");
503-
}
504-
}
478+
import java.io.File;
479+
import java.io.IOException;
480+
481+
public class DeleteFile {
482+
public static void main(String[] args) {
483+
try {
484+
File myFile = new File("myfile.txt");
485+
486+
// Create the file (does nothing if it already exists)
487+
myFile.createNewFile();
488+
System.out.println("File created: " + myFile.getName());
489+
490+
// Delete the file
491+
if (myFile.delete()) {
492+
System.out.println("Deleted " + myFile.getName());
505493
}
494+
} catch (IOException e) {
495+
e.printStackTrace();
496+
}
497+
}
498+
}
499+
506500
</code>
507501
</program>
508502

509503
<p>
510-
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 <c>delete()</c> method. This method will delete any file with the name provided when creating the myFile object. Similar to the <c>createNewFile()</c> method, it will return <c>true</c> if the file existed and could be deleted, and false if the file could not be deleted.
504+
Note that this is almost identical to the code within the <c>try</c> block of the <c>CreateFile </c>class we made earlier. The key difference is the use of the <c>delete()</c> method. This method will delete any file with the name that was linked to the myFile object. Similar to the <c>createNewFile()</c> method, it will return <c>true</c> if the file exists and can be deleted, and <c>false</c> if the file cannot be deleted.
511505
</p>
512506
</section>
513507

0 commit comments

Comments
 (0)