1 FileVisitor 搜尋目錄時不一定會使用什麼順序
2 如果你想刪除某個目錄請在postVisitDirectory方法實作
因為要刪除目錄前目錄的檔案要先移除
3 By default, walkFileTree does not follow symbolic links.
如想跟蹤連結使用
- EnumSet opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
- try {
- Files.walkFileTree(listDir, opts, Integer.MAX_VALUE, walk);
- } catch (IOException e) {
- System.err.println(e);
- }
Integer.MAX_VALUE參數指的是搜尋深度,如果是0不會搜尋指定目錄下的任何檔案與目錄
如果是1會收指定目錄下的那一層如下
dir 為目錄
file_x 為檔案
MyDir
dir_1 dir_2 dir_3 file_1
file_1_2
如設零指會找MyDir下的檔案會搜尋到指會搜尋到有MyDir
指定為1 會找到 dir_1 , dir_2,dir_3,file_1
如果是1會收指定目錄下的那一層如下
dir 為目錄
file_x 為檔案
MyDir
dir_1 dir_2 dir_3 file_1
file_1_2
如設零指會找MyDir下的檔案會搜尋到指會搜尋到有MyDir
指定為1 會找到 dir_1 , dir_2,dir_3,file_1
preVisitDirectory– Invoked before a directory's entries are visited.//顯示檔案之前會先讀取此方法讀取目錄postVisitDirectory– Invoked after all the entries in a directory are visited. If any errors are encountered, the specific exception is passed to the method.- 檔案都讀取好了最後才會讀取此方法
visitFile– Invoked on the file being visited. The file'sBasicFileAttributesis passed to the method, or you can use the file attributes package to read a specific set of attributes. For example, you can choose to read the file'sDosFileAttributeViewto determine if the file has the "hidden" bit set.- 顯示檔案的部份
visitFileFailed– Invoked when the file cannot be accessed. The specific exception is passed to the method. You can choose whether to throw the exception, print it to the console or a log file, and so on.- 如有錯誤會在這處理
preVisitDirectory(T dir, BasicFileAttributes attrs)
visitFile(T file, BasicFileAttributes attrs)
visitFileFailed(T file, IOException exc)
postVisitDirectory(T dir, IOException exc)-->參數是IOExecption
什麼狀況下會傳入ㄌㄟ~當讀取的資料夾或檔案有問題時,如權限問題等等~~
如我將某一個目錄改成700 讀取權限,跑walkFileTree就會進入
當目錄進入visitFileFailed就不會讀取
visitFileFailed postVisitDirectory 內的都會傳入一個IOExecption什麼狀況下會傳入ㄌㄟ~當讀取的資料夾或檔案有問題時,如權限問題等等~~
如我將某一個目錄改成700 讀取權限,跑walkFileTree就會進入
visitFileFailed當目錄進入visitFileFailed就不會讀取
postVisitDirectory
讀取順序
沒有留言:
張貼留言