搜索文件或目录中包含字符串的文件 java小程序

package com.ruishenh.spring.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class SearchFile {

	private static final int DEFAULT_BUF_SIZE = 64;

	private static int searchCount=0;

	public static void main(String[] args) throws IOException {

		String dir,searchStr = null;

		System.out.println("欢迎使用小侯查询文件java程序");

		while (true) {
			System.out.println("请输入查询目录:");
			Scanner sc = new Scanner(System.in);
			dir = sc.next();
			break;
		}
		while (true) {
			System.out.println("请输入查询的关键字:");
			Scanner sc = new Scanner(System.in);
			searchStr= sc.next();
			break;
		}
		System.out.println("查询中......");
		SearchFile sf = new SearchFile();
		sf.searchStrInFile(dir, searchStr, true);
		System.out.println("查询完毕......");
		System.out.println("查询到"+searchCount+"处.");

	}
	//查询字符从jar文件
	void searchStrInJar(String file, String searchStr, int buf) throws IOException {
		JarFile jf = new JarFile(new File(file));
		searchStrInJar(jf, searchStr, buf);
	}
	//查询字符从jar文件
	void searchStrInJar(JarFile jf,String searchStr, int buf) throws IOException {
		Enumeration<JarEntry> jss = jf.entries();
		InputStream in;
		while (jss.hasMoreElements()) {
			JarEntry je = jss.nextElement();
			in = jf.getInputStream(je);
			searchStrInInputStream(jf.getName()+"!/"+je.getName(), searchStr, buf, in);
		}
	}

	void searchStrInTxt(String file, String searchStr, int buf) throws IOException {
		searchStrInTxt(new FileInputStream(new File(file)),file, searchStr, buf);
	}

	//查询字符从普通文件
	void searchStrInTxt(InputStream is, String fileName,String searchStr, int buf) throws IOException {
		searchStrInInputStream(fileName, searchStr, buf, is);
	}
	//查询字符从指定InputStream
	private void searchStrInInputStream(String fileName, String searchStr, int buf, InputStream is) throws IOException{
		searchStrInBufferedReader(fileName, searchStr, buf, new BufferedReader(new InputStreamReader(is)));
	}
	private void searchStrInBufferedReader(String fileName, String searchStr, int buf, BufferedReader r) throws IOException {
		char[] blocks = new char[buf];
		int length = searchStr.length();
		if (buf < length) {
			throw new IOException("读取大小不能小于搜索字符串的长度");
		}
		String preStr = null;
		while (r.read(blocks) != -1) {
			String tempStr = new String(blocks);
			String tmpStr = (preStr + tempStr);
			if (null != tmpStr && tmpStr.indexOf(searchStr) > -1) {
				System.out.println(fileName + "---->" + tmpStr);
				searchCount++;
			}
			if (tempStr.length() > length) {
				preStr = tempStr.substring(tempStr.length() - length);
			} else {
				preStr = tempStr;
			}
		}
	}
	//查询字符从指定文件或目录
	void searchStrInFile(String dir, String searchStr, boolean recurse) {
		try {
			File d = new File(dir);
			if (!d.isDirectory()) {
				return;
			}
			File[] files = d.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (recurse && files[i].isDirectory()) {
					searchStrInFile(files[i].getAbsolutePath(), searchStr, true);
				} else {
					String filename = files[i].getAbsolutePath();
					if (filename.endsWith(".jar")) {
						searchStrInJar(filename, searchStr, DEFAULT_BUF_SIZE);
					}
					if (filename.endsWith(".zip")) {
						searchStrInZip(filename, searchStr, filename);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//查询字符从Zip文件
	private void searchStrInZip(String dir, String searchStr, String filename) throws IOException {
		ZipFile zip = new ZipFile(filename);
		Enumeration<? extends ZipEntry> entries = zip.entries();
		while (entries.hasMoreElements()) {
			ZipEntry entry = entries.nextElement();
			String currFile = entry.getName();
			if (entry.isDirectory()) {
				searchStrInFile(currFile, searchStr, true);
			} else {
				searchStrInInputStream(filename+File.separator+currFile, searchStr, DEFAULT_BUF_SIZE, zip.getInputStream(entry));

				//zip中的文件就不在做具体细化的操作

//				if (currFile.endsWith(".zip")) {
//					searchStrInZip(filename, searchStr,filename+File.separator+currFile);
//				} else if (currFile.endsWith(".jar")) {
//					searchStrInJar(new JarFile(zip.getInputStream(entry)), searchStr, DEFAULT_BUF_SIZE);
//				} else {
//					// 默认按照文本来做
//					searchStrInTxt(currFile, searchStr, DEFAULT_BUF_SIZE);
//				}
			}

		}
	}
}

运行效果:

搜索文件或目录中包含字符串的文件 java小程序,布布扣,bubuko.com

时间: 2024-08-02 06:59:29

搜索文件或目录中包含字符串的文件 java小程序的相关文章

[APUE]文件和目录(中)

一.link.unlink.remove和rename 一个文件可以有多个目录项指向其i节点.使用link函数可以创建一个指向现存文件连接 #include <unistd.h> int link(const char *existingpath, const char *newpath); 返回值:成功为0,出错为-1 该函数创建一个新目录项newpath,指向现存文件existingpath,如果newpath已存在则返回出错. 为了删除一个现存的目录项,可以调用unlnk函数. #inc

【Eclipse】根据文件名查找文件与根据文件中的字符串查找文件

在大型项目开发中,你经常需要在Eclipse左方那庞大的文件树中寻找你需要的文件. 此时,你直接使用Ctrl+Shift+R就可以唤出文件查找窗口.这是根据文件名来找文件,如下图,只要输入你需要寻找的文件名,就能得到相应的寻找结果. 如果你需要根据文件中的字符串查找文件,那么,你可以通过Ctrl+H唤出查找与替换的窗口,切换到File Search选项卡中.输入你要搜索的字符串,与此字符串可能存在的文件后缀名,如果不记得输入*.*,输入文件后缀的原因是为了减少Eclipse的搜索量,加快搜索速度

往jdk/bin目录中增加tcnative-1.dll文件以后报错 Can&#39;t load AMD 64-bit .dll on a IA 32-bit platform

开始时,运行Tomcat控制台报错: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/local/jdk1.6.0_26/jre/lib/i386/server:/usr/local/jdk1.6.0_26/jre/lib/i386:/usr/loca

Android项目在eclipse中无法打包apk文件[bin目录下没生成apk文件]问题解决

后来我发现在eclipse的Preferences -> Android -> Build中有一项“Skip packaging and dexing until export or launch....”,原来这个选项默认是被勾选的,这个选项的意思是“跳过packing和dexing,直到export或者 launch...”,去掉这个选项即可解决问题. Android项目在eclipse中无法打包apk文件[bin目录下没生成apk文件]问题解决

前端技术:HTML---Head标签中包含的头文件标签,body标签包含的内部标签

1.Head标签中包含的 头文件标签的作用: (1)title标签:定义网页的标题. (2)meta标签:一般用于定义页面的特殊信息,例如页面的关键字.页面描述等 (3)link标签:用于引入外部样式文件(CSS 文件). (4)style标签:用于定义元素的CSS样式. (5)script标签:用于定义页面的JavaScript代码,或者引入外部JavaScript文件. meta标签包含的属性 (1)charset属性:字符集编码方式: ASCII:数字.英文字母.字符进行编码 GB2312

在目录下查找包含字符串的文件

编写一个search(s)的函数,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出完整路径: #!/usr/bin/env python import os, os.path import sys def search(path, s):     for x in os.listdir(path):         fp = os.path.join(path, x)         if os.path.isfile(fp) and  s in x:       

通过词法分析实现的指出C程序中包含的头文件

在阅读有些程序的源码时,很希望能够马上弄清楚源码中到底包含了哪些头文件,以确定是否需要为了特殊的函数而手动加入#include.借助flex的词法分析实现了这一功能,本质上就是对正则表达式的匹配.注意这个程序不能够处理包含嵌套的情形(也就是说不能指出包含文件包含了哪些文件),感兴趣的可以通过栈来实现.源码如下: 1 /*源代码:ic.l*/ 2 3 /*定义文件预处理指令文件名起始状态*/ 4 %x IFILE 5 6 %% 7 ^"#"[ \t]*include[ \t]*[\&qu

用JAVA查找指定目录下包含关键字的文件

最近要改一个产品到MYSQL数据库,源代码是遗留下来的,里面有很多反编译的文件,并且带有错误.因此想要删掉这些反编译文件,避免干扰.好在这些文件内容里都带有反编译器的信息.通过关键字在ECLIPSE下搜索发现了500+个文件,无奈只好想办法批量删除.首先想到用脚本语言(bat),网上找了一遍,没有合适的,最后决定用JAVA实现. 1 public static String[] findFiles(String baseURL,String... contains){ 2 File baseDI

VBScript脚本实现在目录中自动获取某个文件

1. 应用场景:通常在开发流上都会保留本版本或历史版本的多个bulid,若需要获取目录中(目录中除了build还有很多其他名称不一样的文件)最新的build安装该如何写脚本实现呢?而且通常build的命名中都会有依次增加的version号,该如何实现脚本可每日重复使用呢? 2.实现思路:1>处理文件名称,定义一个常量文件名,将文件名中的变化的version号用*代替: 2>给定已知的目录,获取给目录下所有的子文件,然后将按*分割,依次匹配子文件的文件名和被分割的这两部分名称. 3.代码如下: