Which options are true for the following code?
public static void check(Path path) throws Exception { if (!Files.exists(path)) { System.out.println("Not exists"); Files.createDirectories(path.getParent()); Files.createFile(path);//from w w w . j a v a 2 s .c om } else if (!Files.notExists(path)) { System.out.println("exists"); Files.delete(path); } else { System.out.println("can never reach here"); } }
check()
will output "Not exists" and try to create a new file, if it doesn't exist.check()
will output "exists" and try to delete the file if it exists. It won't delete a directory.check()
can never output "can never reach here".check()
can throw an IOException, NoSuchFileException.a, d
Option (b) is incorrect because Files.
delete()
can be used to delete files and empty directories.
If you try to delete a nonempty directory, Files.
delete()
will throw a DirectoryNotEmptyException.
Option (c) is incorrect because both exists()
and notExists()
can return false if the underlying system can't determine the existence of a file.