click below
click below
Normal Size Small Size show me how
OCPJP708.3
Cert Obj Read and change file and directory attributes
[8.3.1]objective | Read and change file and directory attributes, focusing on the BasicFileAttributes, DosFileAttributes, and PosixFileAttributes interfaces. |
[8.3.2] Prior to nio.2 we were using File | file.canWrite(), file.canRead() eg File file = new File("test"); file.lastModified(); |
[8.3.3] After Java 7 we can use Files | Path path = Paths.get("test"); Files.getLastModifiedTime(path); |
[8.3.4]BasicFileAttributes | "attributes common to many file systems." What they mean is that you can rely on these attributes being available to you unless you are writing Java code for some funky new operating system. Basic attributes include things like creation date. |
[8.3.5]PosixFileAttributes | POSIX stands for Portable Operating System Interface. This interface is implemented by both UNIX- and Linux-based operating systems. |
[8.3.6]DosFileAttributes | DOS stands for Disk Operating System. It is part of all Windows operating systems. Even Windows 8 has a DOS prompt available. |
[8.3.7]The BasicFileAttributes and BasicFileAttributeView interfaces are a bit confusing. They have similar names but different functionality, and you get them in different ways. Try to remember these three things: | ■ Only BasicFileAttributeView is singular. ■ You get BasicFileAttributeView using Files.getFileAttributeView, BasicFileAttributes using Files.readAttributes.■ You can ONLY update attributes in BasicFileAttributeView, not in BasicFileAttributes. |
[8.3.8]There is an alternative way to set these attributes where you don't have to worry about the String values. However, the exam wants you to know how to use Files. It is good to know both ways, though. | DosFileAttributeView view = Files.getFileAttributeView(path, DosFileAttributeView.class); view.setHidden(true); view.setReadOnly(true); |
[8.3.9] |