查找文件中是否存在指定的单词

来源:互联网 发布:淘宝订单信息查询 编辑:程序博客网 时间:2024/06/03 22:54
package test.com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import com.mysql.jdbc.Connection;
public class test3 {
public static boolean ContainsStr(String s1,String s2){
if(s1.indexOf(s2)>=0){
return true;
}else {
return false;
}
}
public static void main(String[] args) throws IOException {
File f=new File("C:/javacode.txt");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String strRead=null;
List<String> lstLines=new ArrayList<String>();
int nLineNum=1;
while((strRead=br.readLine())!=null){
lstLines.add(strRead);
}
System.out.println("请输入要查找的单词");
Scanner sc=new Scanner(System.in);
String strWord=sc.nextLine();
for(String strTemp:lstLines){
boolean b=ContainsStr(strTemp,strWord);
if(b){
System.out.println(nLineNum+":"+strTemp);
}
nLineNum++;
}
}
}
0 0
原创粉丝点击