Java implements file search in a specified directory

Java implements file search in a specified directory

The search function of the entry Java implementation file is relatively simple, mainly in the following two types:
1. Give the file name, find whether the directory and its subdirectories exist
2. Give the suffix name, find the related files in the directory and its subdirectories

Question type 1:
Question: Find a file in the specified directory, if there is a file to be found in the directory or subdirectory, return 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 第七周第二题 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		System.out.println("请输入要查找的文件名:");
		String filename=in.nextLine();                   //读取要查找的文件名称filename;
		File dir=new File("D:\\desktop\\实验七 数据");      //确定要查找的目录dir;
		boolean flag=false;                              //flag来确定是否查到文件;
		function(filename,dir,flag);                     //function函数递归实现;
		
	}
	static void function(String filename,File dir,boolean flag)
	{
		
		File[]files=dir.listFiles();
		for(File file:files)
		{
			if(file.isDirectory())                      //file是目录时,则重新调用function函数;
				{
					function(filename,file.getAbsoluteFile(),flag);
				}
			if(file.isFile() && filename.equals(file.getName()))  //file时文件且文件名相同时,输出;
			{			
					flag=true;
					System.out.println("要查找的文件路径为:"+file.getAbsolutePath());
					break;
			}
		} 
		if(flag==true)                                 //判断有没有找到过文件;
			return; 
		else
		{
			System.out.print(dir.getName()+"文件夹下没有找到此文件");
			return;
		}
	}

}

The following is the running result:
insert image description here
insert image description here
insert image description here
Question type 2:
* Determine whether there are files with suffixes .java and .jpg in the specified directory and its subdirectories, if so, output the file name and directory path, if not, output "* There are no .java and .jpg files in the directory".

package hello;
import java.util.*;
import java.io.*;
public class 第八周第一题 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("指定的目录为:D:\\desktop");
		String Filename="D:\\desktop";                        //Filename为指定地址;
		File file=new File(Filename);
		boolean flag=false;                                   //flag判断有没有相关文件;
		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为目录时,调用function函数(更新了绝对路径);
			{
				function(m.getAbsolutePath(),m.getAbsoluteFile(),flag);
			}
			if(m.isFile() )                                //m为文件时
			{
				String name=m.getName();
				String houzhui=name.lastIndexOf(".")==-1 ? "" : name.substring(name.lastIndexOf(".")+1);//读取其后缀(java还是jpg)
				if("java".equals(houzhui)||"jpg".equals(houzhui))//判断后缀是不是java或jpg;
				{
					System.out.println("文件"+name+"所在的目录为:"+m.getAbsolutePath());//输出文件的绝对路径;
					flag=true;
				}
			}
		}
		if(flag==true)       
			return;
		else
			System.out.println(Filename+"下没有.java和.jpg文件");
	}
}

Here is the result of running:
insert image description here

Guess you like

Origin blog.csdn.net/ws15168689087/article/details/110233996