<>Java Realize the file search under the specified directory

introduction Java It is relatively simple to realize the file search function , There are mainly the following two kinds :
1. Give file name , Find out if the directory and its subdirectories exist
2. Give suffix , Find related files in a directory and its subdirectories

Question type 1 :
subject : Find a file in the specified directory , If there are files found in the directory or subdirectory , Returns the directory where the file is located , Otherwise output “ There is no such file in the current directory ”.
package hello; import java.io.File; import java.util.*; public class Week 7 question 2 {
public static void main(String[] args) { // TODO Auto-generated method stub
Scanner in=new Scanner(System.in); System.out.println(" Please enter the file name to find :"); String
filename=in.nextLine(); // Read the name of the file to find filename; File dir=new
File("D:\\desktop\\ Experiment 7 data "); // Determine the directory to find dir; boolean flag=false;
//flag To determine if the file was found ; function(filename,dir,flag); //function Recursive implementation of function ; } static void
function(String filename,File dir,boolean flag) { File[]files=dir.listFiles();
for(File file:files) { if(file.isDirectory()) //file When is a directory , Then call again function function ; {
function(filename,file.getAbsoluteFile(),flag); } if(file.isFile() &&
filename.equals(file.getName())) //file File with the same file name , output ; { flag=true;
System.out.println(" The file path to find is :"+file.getAbsolutePath()); break; } }
if(flag==true) // Judge whether the file has been found ; return; else {
System.out.print(dir.getName()+" This file was not found under folder "); return; } } }
Here are the running results :

Question type 2 :
* Judge whether the specified directory and its subdirectories have the suffix .java and .jpg Documents , If so , Output the file name and directory path , without , output “*
Not in directory .java and .jpg file ”.
package hello; import java.util.*; import java.io.*; public class Week 8 question 1 {
public static void main(String[] args) { // TODO Auto-generated method stub
System.out.println(" The specified directory is :D:\\desktop"); String Filename="D:\\desktop";
//Filename Specify an address for ; File file=new File(Filename); boolean flag=false;
//flag Determine whether there are relevant documents ; function(Filename,file,flag); } static void function(String
Filename,File file,boolean flag) { File[]files=file.listFiles(); for(File
m:files) { if(m.isDirectory()) //m When is a directory , call function function ( Updated absolute path ); {
function(m.getAbsolutePath(),m.getAbsoluteFile(),flag); } if(m.isFile() )
//m When is a file { String name=m.getName(); String houzhui=name.lastIndexOf(".")==-1 ?
"" : name.substring(name.lastIndexOf(".")+1);// Read its suffix (java still jpg)
if("java".equals(houzhui)||"jpg".equals(houzhui))// Determine whether the suffix is java or jpg; {
System.out.println(" file "+name+" The directory is :"+m.getAbsolutePath());// Absolute path of the output file ;
flag=true; } } } if(flag==true) return; else
System.out.println(Filename+" Next no .java and .jpg file "); } }
Here are the results of the run :

Technology