Google Code Prettify

2015年7月12日 星期日

NIO.2: 檔案的 metadata 讀寫

昨天說明完 Pro Java 7 NIO.2 的第一章,今天開始說明第二章 (Metadata File Attributes),因為第二章的內容比較多,會分幾次說明,先來看這一段原文的內容:
NIO.2 comes with a set of six views, an overview of which follows:BasicFileAttributeView: This is a view of basic attributes that must be supported by all file system implementations. The attribute view name is basic.DosFileAttributeView: This view provides the standard four supported attributes on file systems that support the DOS attributes. The attribute view name is dos.PosixFileAttributeView: This view extends the basic attribute view with attributes supported on file systems that support the POSIX (Portable Operating System Interface for Unix) family of standards, such as Unix. The attribute view name is posix.FileOwnerAttributeView: This view is supported by any file system implementation that supports the concept of a file owner. The attribute view name is owner.AclFileAttributeView: This view supports reading or updating a file’s ACL. The NFSv4 ACL model is supported. The attribute view name is acl.UserDefinedFileAttributeView: This view enables support of metadata that is user defined
NIO 2 針對檔案系統提供了上述六個 view,可以用來查詢、操作檔案系統的 metadata,不過,每個 OS 的檔案系統組成各不相同,上述六個 view 在程式正執行的那個 OS 上只會有部份是被支援的! 那麼到底 OS 支援那些 view 呢? 可以用這段程式來測試:
 1 package idv.steven.nio2.metadata;
 2 
 3 import java.nio.file.FileSystem;
 4 import java.nio.file.FileSystems;
 5 import java.util.Set;
 6 
 7 public class Determining {
 8 
 9     public static void main(String[] args) {
10         FileSystem fs = FileSystems.getDefault();
11         Set<String> views = fs.supportedFileAttributeViews();
12         for (String view : views) {
13             System.out.println(view);
14         }
15     }
16 }
在 Windows 7 底下執行,輸出如下:
owner
dos
acl
basic
user
在 scientific linux 7.x 下執行,輸出如下:
basic
owner
user
unix
dos
posix
上面執行結果中,紅色標出兩個不同作業系統各自支援的 view,另外,如果想測試出目前的 OS 擁有那些 file store,要用什麼方法? 看一下如下程式:
 1 package idv.steven.nio2.metadata;
 2 
 3 import java.nio.file.FileStore;
 4 import java.nio.file.FileSystem;
 5 import java.nio.file.FileSystems;
 6 import java.nio.file.attribute.BasicFileAttributeView;
 7 
 8 public class Available {
 9 
10     public static void main(String[] args) {
11         FileSystem fs = FileSystems.getDefault();
12         for (FileStore store : fs.getFileStores()) {
13             boolean supported = store.supportsFileAttributeView(BasicFileAttributeView.class);
14             System.out.println(store.name() + " ---" + supported);
15         }
16     }
17 }
在我的 Windows 7 中,因為有 C、D、E 三個磁碟,它們的 volume 分別命名為 System、Document、Software,所以執行結果如下:
System ---true
Document ---true
Software ---true
在 scientific linux 7.x 執行結果會如何呢? 在了解執行結果前,請先在命令列中下 df -a 的指令,用這指令輸出的結果和上述程式的結果比較就知道是什麼了。基本上,linux 上我們會將整顆硬碟劃分成比較多的 partition。



沒有留言:

張貼留言