/**
* fileName 要查找的文本,str要查找扒型的字符串
**/春老猜
public int findString(String fileName,String str){
int count = 0
try {
int line = 0//记录行数含轿
BufferedReader br = new BufferedReader(new FileReader(fileName))
String tmp = br.readLine()
while(tmp!=null){
line ++
int index = tmp.indexOf(str, 0)
while(index>=0){
count ++
System.out.println("在"+line+"行,第"+index+"列,找到字符:"+str)
index = tmp.indexOf(str, index+str.length())
}
tmp = br.readLine()
}
br.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
return count
}
}
*** 作文件用IO啊,遍历文野弯搭件列表,然后把符合条件的保存下来,如果你想做强大点,比如像百度一样的话,建议你去学习下Lucene框架,那速度可不是一般的颂拿惊人闹喊啊,Oracle都无法跟他匹敌!import java.io.Fileimport java.io.FileNotFoundException
import java.io.FileReader
import java.io.IOException
public class FileSearch {
static int mount=0
public static void main(String[] args) {
if(args.length>0){
String filename="\\"
File file=new File(filename)
findFile(file,args[0])
printResult(args[0])
}
}
static boolean isTureFile(File file) {
if (!file.exists()) {
return false
}
if (!file.canRead()) {
return false
}
if (file.getName().startsWith(".")) {
return false
}
if (file.getName().endsWith(".")) {
return false
}
return true
}
public static void findFile(File file,String word) {
File[] listFiles = file.listFiles()
for (int i = 0i <listFiles.lengthi++) {
if (listFiles[i].isDirectory()) {
findFile(listFiles[i],word)
} else if (isTureFile(listFiles[i])) {
SearchWords(listFiles[i],word)
}
}
}
public static void SearchWords(File file,String word) {
try {
int j = 0, k = 0
String str = null
FileReader in = new FileReader(file)
int ch = 0
while ((ch = in.read()) != -1) {
str += (char) ch
}
if (str != null) {
while (str.indexOf(word, j) != -1) {
k += 1
j = str.indexOf(word, j) + 1
}
}
if (k >0) {
System.out.println("在\液丛"" + file.getAbsolutePath() + "\" 有" + k + "处")
mount++
}
in.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
}
public static void printResult(String word) {
if (mount != 0) {
System.out.println("---------------------------------------------\n")
System.out.println("共找到" + mount + "个文本汪余包含关键字困埋滚 \""
+ word + "\"!\n")
mount = 0
} else {
System.out.println("没有找到符合要求的文本!")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)