java2019. 9. 21. 23:33

 

 

1, 해당 interface를 구현하고, accept를 override하면 File 객체의 listFiles 메소드를 호출할 때 Filter로 사용가능.
2, 경로 지정시, separator 사용하는 부분은 기억.
String pathName = "C:"+ File.separator+"Users"+File.separator +"Lee" + File.separator + "test";

 

ex)

package com.test.testapplication.file;

import java.io.File;

public class FileFilterSample {

	public FileFilterSample() {
		super();
		String pathName = "C:"+ File.separator+"Users"+File.separator +"Lee" + File.separator + "test";
		//System.out.println(pathName);
		this.sampleTest(pathName);
	}
	
	public void sampleTest(String pathName) {
		File file; 
		
		try {
			file = new File(pathName);
			//File [] mainFileList = file.listFiles();
			//File [] mainFileList = file.listFiles( new PNGFileNameFilter());
			File [] mainFileList = file.listFiles( new PNGFileFilter());
			
			for (File tempFile : mainFileList) {
				System.out.println(tempFile.getName());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} 
	
}


package com.test.testapplication.file;

import java.io.File;
import java.io.FileFilter;

public class PNGFileFilter implements FileFilter {
	
	@Override
	public boolean accept(File file) {
	
		if (file.isFile()) {
			String fileName = file.getName();
			if ( fileName.endsWith(".png")) 
				return true;
			 else 
				return false;
		}
		return false;
		
	}

}

package com.test.testapplication.file;

import java.io.File;
import java.io.FilenameFilter;

public class PNGFileNameFilter implements FilenameFilter {
	
	
	@Override
	public boolean accept(File dir, String fileName) {
		 
		
		
		if ( fileName.endsWith(".png"))
			return true;
		
		
		
		return false;
	}

}

'java' 카테고리의 다른 글

FileReader 및 BufferedReader + (Scanner)  (0) 2019.09.22
FileWriter 및 BufferedWriter 텍스트기반 처리  (0) 2019.09.22
Thread interrupt 및 getState 예제  (0) 2019.09.21
synchronized 정리  (0) 2019.09.21
Thread Priority 와 Daemon Thread  (0) 2019.09.17
Posted by easy16