标签:搜索jar中的包含字符串的类 搜索指定目录包含字符串的文件 搜索文件小工具
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
标签:搜索jar中的包含字符串的类 搜索指定目录包含字符串的文件 搜索文件小工具
原文地址:http://blog.csdn.net/ruishenh/article/details/34087589