Assume the /environment directory exists and contains a file with a symbolic link to the /environment directory.
In addition, assume all files within the directory are fully accessible.
What is the result of executing the following program?.
import java.nio.file.*; import java.nio.file.attribute.*; public class Main { public static void accessFile(Path p, long timeEpoch) { try { /*www . j a v a 2 s.c om*/ Files.readAttributes(p, BasicFileAttributes.class) .setTimes(null, null, FileTime.fromMillis(timeEpoch)); } catch (Throwable e) { } finally {} } public static final void main(String[] unused) throws Exception { Path w = Paths.get("/environment"); Files.walk(w) .forEach(q -> accessFile(q,System.currentTimeMillis())); } }
A.
The code does not compile because BasicFileAttributes is used to read file information, not write it, making Option A the correct answer.
If the code was changed to pass BasicFileAttributeView.class to the Files.getFileAttributeView()
method, then the code would compile, and Option B would be the correct answer.
Finally, remember that by default, symbolic links are not traversed by the Files.walk()
method, avoiding a cycle.
If FileVisitOption.FOLLOW_LINKS was passed, then the class would throw an exception because Files.walk()
does keep track of the files it visits and throws a FileSystemLoopException in the presence of a cycle.