CodeForcess 220B Little Elephant and Array (Mo Team)

2023-01-23   ES  

java filefilter, Filenamefilter implementation file finding/search function

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() + "No existence" );
			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 does not exist
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 );
	}
}
---------------Many java files

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 );
	}
}
---------------Many java files
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 );
	}
}
---------------Many java files

source

Related Posts

jQuery draws a large turntable instance (plug -in thought)

Spring Boot database Druid password encryption

The Road to Domestic Exploration — Winning Kirin System (VMware Virtual Machine Installation + Real Machine Installation)

front-end foundation-HTML

CodeForcess 220B Little Elephant and Array (Mo Team)

Random Posts

leetcode 119. Yang Hui triangle II

shell script -memory monitoring automatic email alarm

MySQL Explain detailed introduction

wp listbox traversing datatemplate (get all controls)

[Dry -sharing] Teacher Wu Enda’s video tutorial