Interview Questions

Java Keywords
Key Concepts
OOPS in java
Java Collections#1
Java Collections #2
Exceptions #1
Exceptions #2
Threads#1
Threads#2
InnerClass
Serialization
Immutable Class
Cloning in Java
Garbage Collection
JSP #1
JSP #2
Programming Q#1
Programming Q#2
Programming Q#3


Implement FileFilter interface to filter files in java.

The FileFilter interface in java can be used to filter the files from a given directory.For e.g in the example below only files with .html will be read.
 
public class ReadWriteFile1 {

	public static void main(String[] args) {

		File folder = new File("C:\\java-website\\");
	    File[] listOfFiles = folder.listFiles(new FilterHtmlFile());
		String file="";

	    for (int i = 0; i < listOfFiles.length; i++) {
	      if (listOfFiles[i].isFile()) {
	    	  file=listOfFiles[i].getName();
				System.out.println("File " + file );
			}
		}
	}

	class FilterHtmlFile implements FileFilter{

		public boolean accept(File arg0) {
			String fileName = arg0.getName();
			return fileName.endsWith(".html"); // filter the name whic u want. if u want 
			// more than one type of file use regular exp.
		}
	
}