Which of the following statements about the deleteTree()
method is correct?
public void deleteTree(File f) { if(!f.isDirectory()) f.delete(); else { Stream.of(f.list()) .forEach(s -> deleteTree(s)); f.deleteDirectory(); } }
C.
The code contains two compilation errors.
First, the File list()
method returns a list of String values, not File values, so the call to deleteTree()
with a String value does not compile.
Either the call would have to be changed to f.listFiles()
or the lambda expression body would have to be updated to deleteTree
(new File(s)) for the code to work properly.
Next, there is no deleteDirectory()
method in the File class.
Directories are deleted with the same delete()
method used for files, once they have been emptied.