Which method is used to delete a file in Java using the `File` class?
Analysis & Theory
`File.delete()` deletes the file or directory if it exists and returns `true` if successful.
What does `file.delete()` return if the file doesn't exist?
Analysis & Theory
`delete()` returns `false` if the file does not exist or cannot be deleted.
Which package provides the `File` class in Java?
Analysis & Theory
The `File` class is part of the `java.io` package.
What happens if you try to delete a non-empty directory using `File.delete()`?
A
It deletes all files inside it
D
It deletes the directory
Analysis & Theory
`File.delete()` returns `false` if the directory is not empty—it only deletes empty directories.
How can you delete a file in Java using `java.nio.file` package?
Analysis & Theory
`Files.delete(Path)` deletes the file or throws an exception if it doesn't exist.
What exception does `Files.delete(Path)` throw if the file doesn’t exist?
Analysis & Theory
`NoSuchFileException` is thrown if the file specified in the `Path` does not exist.
Which method can delete a file without throwing an exception in `java.nio`?
Analysis & Theory
`Files.deleteIfExists(Path)` deletes a file if it exists, and doesn't throw an exception if it doesn't.
Can you delete a file while it's open by another process in Java?
Analysis & Theory
On most Linux systems, open files can still be deleted, but not on Windows, where a file lock will prevent it.
What is the safest way to delete files recursively in Java?
A
File.delete() in a loop
B
Files.walkFileTree() with FileVisitor
D
System.deleteRecursive()
Analysis & Theory
`Files.walkFileTree()` with a custom `FileVisitor` is the recommended and safe method for recursive deletion.
What should you do before deleting a file to ensure safety?
B
Check if it exists and is writable
Analysis & Theory
Always check if the file exists and is writable before deletion to prevent errors or data loss.