import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day11_A {
private static Scanner sc=null;
public static void main(String[] args) {
sc=new Scanner(System.in);
File file=new File("K:/Test/TestRead.txt");
String str=null,src=null;
if(file.canExecute()) {
str=readFile(file);
}else {
System.out.println("文件不存在!");
return;
}
while(true) {
System.out.println("请输入想查找的单词:over为结束查找!");
src=sc.nextLine();
if(src.equalsIgnoreCase("over"))
break;
System.out.println("查找结果:"+look(src,str));
}
}
private static String readFile(File file){
BufferedReader br=null;
StringBuilder stu=new StringBuilder();
try {
br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
for(String str=br.readLine();str!=null;str=br.readLine()) {
stu.append(str);
stu.append(System.lineSeparator());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
if(br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stu.toString();
}
private static int look(String regex,String str) {
Matcher mat=Pattern.compile(regex).matcher(str);
int count=0;
while(mat.find()) {
count++;
}
return count;
}
}
兄弟
Java——从入门到入土
当码农没有煎荷包蛋有前途