What is the result of executing the following code? (Choose all that apply.)
1: import java.io.IOException; 2: import java.nio.file.*; 3: import java.nio.file.attribute.*; 4: public class Main extends SimpleFileVisitor<Path> { 5: private String extension; 6: public Main(String extension) { 7: this.extension = extension; 8: } /*from www.j av a 2s.c o m*/ 9: 10: public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 11: throws IOException { 12: if(file.getFileName().endsWith(extension) && !attrs.isSymbolicLink()) 13: Files.delete(file); 14: return FileVisitResult.CONTINUE; 15: } 16: 17: public static void main(String[] args) throws IOException { 18: Files.walkFileTree(Paths.get("/p1/data"), 19: new Main("txt")); 20: } 21: }
B,F.
The code compiles without issue, so C, D, and E are each incorrect.
Because a subclass of FileVisitor was used and not the DirectoryStream class, the code will recursively delete all matching files in the /p1/data directory, so B is correct and A is incorrect.
Finally, if the directory path does not exist or is not accessible in the file system, an exception will be thrown at runtime, so F is also correct.