java FileFilter、FilenameFilter实现文件查找/搜索功能

java FileFilter、FilenameFilter实现文件查找/搜索功能

  • FileFilter实现
  • FilenameFilter实现

FileFilter实现

import java.io.*;
class SearchFiles{
	public static void searchFiles( String fileName, String keyword ){
		searchFiles( new File( fileName), keyword );
	}
	public static void searchFiles( File file, final String keyword ){
		if( ! file.exists() ){
			System.out.println( file.getAbsolutePath() + "不存在" );
			return;
		}
		File[] files = file.listFiles( new FileFilter(){
			public boolean accept( File fileName ){
				if( fileName.isDirectory() || fileName.getName().toLowerCase().contains( keyword.toLowerCase() ) ){
					return true;
				}
				return false;
			}
		});
		if( files != null ){
			for( File f : files ){
				if( f.isDirectory() )	searchFiles( f, keyword );
				else	System.out.println( f.getAbsolutePath() );
			}
		}
	}	
	public static void main( String[] args ){
		searchFiles( ".txt", ".txt" );		
	}
}
public class Test0613{
	public static void main( String[] args ){
		SearchFiles.main( args );
	}
}
----------------------
E:\java>java Test0613
E:\java\.txt不存在
import java.io.*;
import java.util.*;
class SearchFiles{
	public static List< File > searchFiles( String fileName, String keyword ){
		return searchFiles( new File( fileName ), keyword );
	}
	public static List< File > searchFiles( File file, final String keyword ){
		if( ! file.exists() )	return null;
		List< File > list = new ArrayList< File >();
		File[] files = file.listFiles( new FileFilter(){
			public boolean accept( File fileName ){
				if( fileName.isDirectory() || fileName.getName().toLowerCase().contains( keyword.toLowerCase() ) ){
					return true;
				}
				return false;
			}
		});
		if( files != null ){
			for( File f : files ){
				if( f.isDirectory() )	list.addAll( searchFiles( f, keyword ) );
				else	list.add( f );
			}
		}
		return list;
	}
	public static void main( String[] args ){
		List< File > list = searchFiles( "E:/java", ".java" );
		if( list != null )
			for( File f : list )	System.out.println( f.getAbsolutePath() );
	}
}
public class Test0613{
	public static void main( String[] args ){
		SearchFiles.main( args );
	}
}
---------------
很多的java文件

FilenameFilter实现

import java.io.*;
class SearchFiles{
	public static void searchFiles( String fileName, String keyword ){
		searchFiles( new File( fileName ), keyword );
	}
	public static void searchFiles( File file, final String keyword ){
		if( ! file.exists() )	return;
		File[] files = file.listFiles( new FilenameFilter(){
			public boolean accept( File dir, String name ){
				File file = new File( dir, name );
				if( file.isDirectory() || file.getName().toLowerCase().contains( keyword.toLowerCase() ) ){
					return true;
				}
				return false;
			}
		});
		if( files != null ){
			for( File f : files ){
				if( f.isDirectory() )	searchFiles( f, keyword );
				else	System.out.println( f.getAbsolutePath() );	
			}
		}
	}
	public static void main( String[] args ){
		searchFiles( "E:/java", ".java" );
	}
}
public class Test0614{
	public static void main( String[] args ){
		SearchFiles.main( args );
	}
}
---------------
很多的java文件
import java.io.*;
import java.util.*;
class SearchFiles{
	public static List< File > searchFiles( String fileName, String keyword ){
		return searchFiles( new File( fileName ), keyword );
	}
	public static List< File > searchFiles( File file, final String keyword ){
		if( ! file.exists() )	return null;
		List< File > list = new ArrayList< File >();
		File[] files = file.listFiles( new FilenameFilter(){
			public boolean accept( File dir, String name ){
				File file = new File( dir, name );
				if( file.isDirectory() || file.getName().toLowerCase().contains( keyword.toLowerCase() ) ){
					return true;
				}
				return false;
			}
		});
		if( files != null ){
			for( File f : files ){
				if( f.isDirectory() )	list.addAll( searchFiles( f, keyword ) );
				else	list.add( f );
			}
		}
		return list;
	}
	public static void main( String[] args ){
		List< File > list = searchFiles( "E:/java", ".java" );
		if( list != null )
			for( File f : list )	System.out.println( f.getAbsolutePath() );
	}
}
public class Test0614{
	public static void main( String[] args ){
		SearchFiles.main( args );
	}
}
---------------
很多的java文件

你可能感兴趣的:(java随记)