java实现文件的搜索和读取


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class test04 {

	/*文件的读取
         *f  传入一个文件对象
         */

	public void readFile(File f) throws IOException{
		
		FileInputStream in=new FileInputStream(f);
		
		BufferedReader bf=new BufferedReader(new InputStreamReader(in));
		String str=null;
		
		String date;
		
		while((str=bf.readLine())!=null){
			
		       //搜索字符串不包含某个字符串
			if(!str.contains("**")&&!str.contains("**")){
				date=str;
				System.out.println(date
						);
				
			}
			
			if(str.contains("**")&&str.contains("**")){
				
				System.out.println(str);
				
				break;
			}
			
			
			
			
		}
		

		
	}
	
	
	 /** 
     * 递归查找文件 
     * @param baseDirName  查找的文件夹路径 
     * @param targetFileName  需要查找的文件名 
     * @param fileList  查找到的文件集合 
     */  
    public static void findFiles(String baseDirName, String targetFileName, List fileList) {  
        /** 
         * 算法简述: 
         * 从某个给定的需查找的文件夹出发,搜索该文件夹的所有子文件夹及文件, 
         * 若为文件,则进行匹配,匹配成功则加入结果集,若为子文件夹,则进队列。 
         * 队列不空,重复上述操作,队列为空,程序结束,返回结果。 
         */  
        String tempName = null;  
        //判断目录是否存在  
        File baseDir = new File(baseDirName);  
        if (!baseDir.exists() || !baseDir.isDirectory()){  
            System.out.println("文件查找失败:" + baseDirName + "不是一个目录!");  
        } else {  
            String[] filelist = baseDir.list();  
            for (int i = 0; i < filelist.length; i++) {  
                File readfile = new File(baseDirName + "\\" + filelist[i]);  
                //System.out.println(readfile.getName());  
                if(!readfile.isDirectory()) {  
                    tempName =  readfile.getName();   
                    if (test04.wildcardMatch(targetFileName, tempName)) {  
                        //匹配成功,将文件名添加到结果集  
                        fileList.add(readfile.getAbsoluteFile());   
                    }  
                } else if(readfile.isDirectory()){  
                    findFiles(baseDirName + "\\" + filelist[i],targetFileName,fileList);  
                }  
            }  
        }  
    }  
      
    /** 
     * 通配符匹配 
     * @param pattern    通配符模式 
     * @param str    待匹配的字符串 
     * @return    匹配成功则返回true,否则返回false 
     */  
    private static boolean wildcardMatch(String pattern, String str) {  
        int patternLength = pattern.length();  
        int strLength = str.length();  
        int strIndex = 0;  
        char ch;  
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {  
            ch = pattern.charAt(patternIndex);  
            if (ch == '*') {  
                //通配符星号*表示可以匹配任意多个字符  
                while (strIndex < strLength) {  
                    if (wildcardMatch(pattern.substring(patternIndex + 1),  
                            str.substring(strIndex))) {  
                        return true;  
                    }  
                    strIndex++;  
                }  
            } else if (ch == '?') {  
                //通配符问号?表示匹配任意一个字符  
                strIndex++;  
                if (strIndex > strLength) {  
                    //表示str中已经没有字符匹配?了。  
                    return false;  
                }  
            } else {  
                if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {  
                    return false;  
                }  
                strIndex++;  
            }  
        }  
        return (strIndex == strLength);  
    }  
	
	
	
	
	public static void main(String[] args) {
		
//		
//		try {
//			new test04().readFile(new File("image/161020.txt"));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		  String baseDIR = "image";   
	        //    找扩展名为txt的文件  
	        String fileName = "*.txt";   
	        List resultList = new ArrayList();  
	        test04.findFiles(baseDIR, fileName, resultList);   
	        if (resultList.size() == 0) {  
	            System.out.println("No File Fount.");  
	        } else {  
	            for (int i = 0; i < resultList.size(); i++) {  
	                //System.out.println(resultList.get(i));//显示查找结果。   
	                String s=resultList.get(i)+"";
	                try {
						new test04().readFile(new File(s));
						
						
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	                
	            }  
	        }  
		
		
		
	}
	
	
}

 

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现批量搜索Java文件内容的方法有很多种,以下是其中一种方法: 1. 遍历指定文件夹下的所有Java文件; 2. 依次打开每个Java文件; 3. 读取文件内容,逐行搜索目标关键词; 4. 如果关键词匹配成功,记录下该文件的路径; 5. 搜索完所有Java文件后,返回所有匹配到的文件路径。 具体的实现步骤如下: 1. 使用Java文件操作类(如File类)遍历指定文件夹下的所有文件文件夹; 2. 对于遍历到的每个文件,判断它是否为Java文件,可以通过文件的后缀名来判断,比如以".java"结尾; 3. 对于是Java文件文件,使用Java的IO流(如BufferedReader类)打开文件; 4. 逐行读取文件内容,可以使用readLine()方法; 5. 对于每一行读取到的内容,使用String类的indexOf()方法或contains()方法判断目标关键词是否存在; 6. 如果存在目标关键词,记录下该Java文件的路径; 7. 继续读取下一行,直到文件结束; 8. 关闭文件,释放资源; 9. 继续遍历下一个文件,重复步骤3-8; 10. 所有Java文件遍历完毕后,返回所有匹配到的文件路径。 需要注意的是,实现过程中要考虑文件过大或者文件数目过多的情况,可以使用递归方式对文件夹进行遍历,以减少内存占用。此外,还需要考虑多线程的应用,实现并发搜索来提高搜索速度。 以上就是实现批量搜索Java文件内容的一种方法,可以根据具体的需求进行适当的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值