实现类:在给定目录下搜索包含给定关键字的文件和文件夹

本文介绍了一个名为SeekFile的Java类,该类用于搜索指定目录下含有特定关键字的文件和文件夹。文章详细记录了在开发过程中遇到的空指针异常问题及其解决方法,并强调了检查文件隐藏属性的重要性。
在写SeekFile类之前学习了下File类,熟悉了下用到的几个类的用法。
接近尾声的时候,被异常所困扰。在不断调试中,终于发现,在E:\下有隐藏文件夹,导致抛出异常(见标记处):
Exception in thread "main" java.lang.NullPointerException
最后把 if (listPathNameArray[index].isDirectory())
修改为 if (listPathNameArray[index].isDirectory() && !listPathNameArray[index].isHidden())后终于正常了。
java.lang.NullPointerException异常(空指针异常):
指当应用程序试图在需要对象的地方使用null时,抛出空指针异常。这种情况包括调用了未被初始化的对象或不存在的对象。
//
再次寻找抛出空指针异常的原因,目标锁定在文件夹/文件访问权限上(PS:我的是win7系统)。先mark一下。
//
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

/**
 * SeekFile类:在给定目录下搜索包含给定关键字的文件和文件夹
 */
public class SeekFile/* implements Filter */
{
	private String pathname;			// 指定目录
	private CharSequence charSequence;	// 指定关键字
	private Vector<File> listFitFileArray = new Vector<File>();	// 符合要求的文件名
	
	public SeekFile()
	{}
	
	public SeekFile(String pathname, CharSequence charSequence)
	{
		this.pathname = pathname;
		this.charSequence = charSequence;
	}
	
	// 得到符合要求的路径名数组
	public Vector<File> getTheFitFileName()
	{
		BreadthFirstSearch();		// 调用广搜方法
		
		return listFitFileArray;	// 返回最终结果
	}
	
	// 广搜
	private void BreadthFirstSearch()
	{
		Queue<String> queue = new LinkedList<String>();
		
		queue.add(pathname);
		
		while (0 != queue.size())
		{
			File newPathname = new File(queue.remove());		// 当前目录
			File[] listPathNameArray = newPathname.listFiles();	// 把当前目录下的file和directory存到数组
			
			SelectTheFitFileNameWithSuffix(listPathNameArray);	// 挑选符合要求的file的路径名
				
			// 目录入队
			for (int index = 0; index < listPathNameArray.length; index++)
			{
				if (!listPathNameArray[index].isHidden()		// 筛选目录且非隐藏
						&&listPathNameArray[index].isDirectory()) 	// 是否是一个目录
				{
					queue.offer(listPathNameArray[index].toString());	// 这句一直引起异常,标记下
				}
			}
		}
	}
	
	// 挑选符合要求的路径名
	private void SelectTheFitFileNameWithSuffix(File[] listPathNameArray)
	{
		for (int index = 0; index < listPathNameArray.length; index++)
		{
			if (listPathNameArray[index].isFile() 			// 是一个标准文件
					&& !listPathNameArray[index].isHidden()	// 非隐藏
					&& listPathNameArray[index].toString().contains(charSequence))	// 包含关键字
			{
				listFitFileArray.add(listPathNameArray[index]);
			}
		}
	}
	
}
import java.io.File;
import java.util.Scanner;
import java.util.Vector;

public class TestSeekFile 
{

	/**
	 * @param args
	 * @author 塘里的潜艇
	 */
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		
		System.out.println("输入目录  关键字");
		
		String pathname = sc.next();			// 指定目录
		CharSequence charSequence = sc.next();	// 指定关键字
		SeekFile cf = new SeekFile(pathname, charSequence);
		Vector<File> fileArray = cf.getTheFitFileName();	// 接收符合要求的路径名数组

		// 依次打印
		for (int index = 0; index < fileArray.size(); index++)
		{
			System.out.println(fileArray.get(index));
		}
	}
}

转载于:https://www.cnblogs.com/submarinex/archive/2011/03/28/1998070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值