iT邦幫忙

DAY 24
2

java菜鳥的學習筆記系列 第 24

使用java NIO2-SimpleFileVisitor-Part 2

  • 分享至 

  • xImage
  •  

之前已經分享過在一個目錄搜尋,但只有用到其中一個visitFile方法,今天分享用SimpleFileVisitor走訪底下所有目錄與檔案,主要要覆寫SimpleFileVisitor中的四個方法preVisitDirectoryvisitFilevisitFileFailedpostVisitDirectory,除了visitFileFailed沒有研究外,其他稍有了解,preVisitDirectory方法用白話來說就是進入目錄前要做哪些事,比如說訂定規則那些目錄名稱不進入,相對的postVisitDirectory就是離開目錄的時候要做那些事,比如說COPY完檔案了,裡面的檔案可以刪除之類的,visitFile顧名思義就是在資料夾中走訪各個檔案,所以流程應該是這樣的:preVisitDirectory->visitFile->postVisitDirectory,另外一提的是FileVisitResult的enum參數可以用來控制目錄走訪流程,今日自己寫的code如下。

參考網站:http://docs.oracle.com/javase/tutorial/essential/io/walk.html

package SimpleVistor;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.FileVisitResult.*;

public class MyDir {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
					Path path;
					try{
						path=Paths.get("D:\\saveall20121020\\1aw010");
						Files.walkFileTree(path, new ListAll()); 
						
					}catch(IOException e){
						e.printStackTrace();
					}
					
	}
   private static class ListAll extends SimpleFileVisitor<Path>{ 

	@Override
	public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)//將進入當下目錄時,
			throws IOException {                                                                                             //做以下動作
		// TODO Auto-generated method stub
		
		if(dir.toString().contains("_")){ 
		System.out.println("Not Entering Directory:"+dir.getFileName());
		return FileVisitResult.SKIP_SUBTREE; //FileVisitResult相關參數是用來控制走訪流程
		}                                                                       //這個參數代表子目錄不再進入,不會呼叫postVisitDirectory
		System.out.println("Entering Directory:"+dir.getFileName());
		return FileVisitResult.CONTINUE; //繼續走訪
	}

	@Override
	public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)//掃描檔案
			throws IOException {
		// TODO Auto-generated method stub
		if(file.toString().endsWith(".xml")) {
		System.out.println("Files Name is:"+file.getFileName());//只輸出xml檔名
		return FileVisitResult.CONTINUE; 
		}
		return FileVisitResult.SKIP_SUBTREE; //代表如果一個xml也沒有,
	}																			//這個目錄以及以下的目錄都不進去了

	@Override
	public FileVisitResult visitFileFailed(Path file, IOException exc)//當檔案無法讀取時,採取什麼動作
			throws IOException {                                                                       //還沒研究是否有實際應用
		// TODO Auto-generated method stub
		System.out.println(file.getFileName()); //
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult postVisitDirectory(Path dir, IOException exc)//離開當下目錄的時候
			throws IOException {                                                                               //執行以下動作
		// TODO Auto-generated method stub
		System.out.println("Exiting Directory:"+dir.getFileName());
		return FileVisitResult.CONTINUE;
	}
	   	
   }

}

Output:

Entering Directory:1aw010
Entering Directory:102112
Not Entering Directory:1C51_1
Not Entering Directory:1C51_2
Not Entering Directory:1C74_1
Not Entering Directory:1C74_10
Not Entering Directory:1C74_11
Not Entering Directory:1C74_12
Not Entering Directory:1C74_2
Not Entering Directory:1C74_3
Not Entering Directory:1C74_4
Not Entering Directory:1C74_5
Not Entering Directory:1C74_6
Not Entering Directory:1C74_7
Not Entering Directory:1C74_8
Not Entering Directory:1C74_9
Not Entering Directory:1H23_1
Not Entering Directory:1H23_2
Not Entering Directory:1N31_2
Not Entering Directory:1N31_3
Not Entering Directory:1N31_4
Not Entering Directory:1N31_5
Not Entering Directory:1N31_6
Not Entering Directory:all_icc_params
Entering Directory:devices
Files Name is:1C51_403103.xml
Files Name is:1C51_403104.xml
Files Name is:1C51_403203.xml
Files Name is:1C51_403204.xml
Files Name is:1C74_402103.xml
Files Name is:1C74_402104.xml
Files Name is:1C74_402203.xml
Files Name is:1C74_402303.xml
Files Name is:1C74_402403.xml
Files Name is:1C74_402404.xml
Files Name is:1C74_402503.xml
Files Name is:1C74_402504.xml
Files Name is:1C74_402603.xml
Files Name is:1C74_402703.xml
Files Name is:1C74_402803.xml
Files Name is:1C74_402903.xml
Files Name is:1C74_402A03.xml
Files Name is:1C74_402B03.xml
Files Name is:1C74_402B04.xml
Files Name is:1C74_402C03.xml
Files Name is:1n31_2.xml
Files Name is:1n31_3.xml
Files Name is:1n31_4.xml
Files Name is:1n31_5.xml
Files Name is:1n31_6.xml
Not Entering Directory:1N31_IFM old files
Files Name is:1P16.xml
Files Name is:1P16_old.xml
Files Name is:1P16_orig.xml
Files Name is:1P16_wo_OPCServer.xml
Files Name is:1P16_w_OPCServer.xml
Files Name is:404105.xml
Files Name is:404106.xml
Files Name is:40410b.xml
Files Name is:404205.xml
Files Name is:40420b.xml
Files Name is:40420d.xml
Files Name is:Copy of 40420d.xml
Files Name is:Copy of G63.xml
Files Name is:G31.xml
Files Name is:G62.old.xml
Files Name is:G62.xml
Files Name is:G63.xml
Files Name is:P28a.xml
Files Name is:P28b.xml
Files Name is:P28c.xml
Files Name is:P28d.xml
Files Name is:P28e.xml
Files Name is:P28f.xml
Not Entering Directory:txt_files
Entering Directory:unused.xml
Files Name is:1P16.actual.xml
Files Name is:1P16.test.xml
Files Name is:1P16.xml
Files Name is:1P16prev.xml
Files Name is:401303.xml
Files Name is:401304.xml
Files Name is:401305.xml
Files Name is:401306.xml
Files Name is:Ifm4.xml
Files Name is:Mhi_port.xml
Files Name is:No_dhcp.xml
Files Name is:OPCDevExample.xml
Files Name is:Plc5.xml
Files Name is:Rciscfg.xml
Files Name is:Redether.xml
Exiting Directory:unused.xml
Entering Directory:used.xml
Files Name is:1C51_403103.xml
Files Name is:1C51_403104.xml
Files Name is:1C51_403203.xml
Files Name is:1C51_403204.xml
Files Name is:1C74_402103.xml
Files Name is:1C74_402104.xml
Files Name is:1C74_402203.xml
Files Name is:1C74_402303.xml
Files Name is:1C74_402403.xml
Files Name is:1C74_402404.xml
Files Name is:1C74_402503.xml
Files Name is:1C74_402504.xml
Files Name is:1C74_402603.xml
Files Name is:1C74_402703.xml
Files Name is:1C74_402803.xml
Files Name is:1C74_402903.xml
Files Name is:1C74_402A03.xml
Files Name is:1C74_402B03.xml
Files Name is:1C74_402B04.xml
Files Name is:1C74_402C03.xml
Files Name is:1n31_2.xml
Files Name is:1n31_3.xml
Files Name is:1n31_4.xml
Files Name is:1n31_5.xml
Files Name is:1n31_6.xml
Files Name is:404105.xml
Files Name is:404106.xml
Files Name is:40410b.xml
Files Name is:404205.xml
Files Name is:40420b.xml
Files Name is:40420d.xml
Files Name is:G31.xml
Files Name is:G62.xml
Files Name is:G63.xml
Files Name is:P28a.xml
Files Name is:P28b.xml
Files Name is:P28c.xml
Files Name is:P28d.xml
Files Name is:P28e.xml
Files Name is:P28f.xml
Exiting Directory:used.xml
Not Entering Directory:ziph_files
Exiting Directory:devices
Entering Directory:sequeninclude
Exiting Directory:sequeninclude
Entering Directory:sequenlibrary
Exiting Directory:sequenlibrary
Entering Directory:sequenskeleton
Exiting Directory:sequenskeleton
Exiting Directory:102112
Exiting Directory:1aw010


上一篇
使用Apache Commons-Super CSV-讀取csv檔
下一篇
使用JAXB-JAXBContext將POJO轉換成XML格式
系列文
java菜鳥的學習筆記28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

我要留言

立即登入留言