写一个程序,从文件(c:\test.txt)中查出字符串”mobnet”出现的次数?

package demo_io;


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class TxtCount {

public static void main(String[] args) throws Exception {
//读入文本(字符),用FileReader->BufferedReader
//读入输入(字节),用FileInputStream->InputStreamReader->BufferedReader
BufferedReader br = new BufferedReader(new FileReader("d:\\desktop\\1.txt"));
StringBuilder sb = new StringBuilder();


while(true){
String str = br.readLine();
if(str == null){
break;
}
sb.append(str);
}
Pattern p = Pattern.compile("mobnet");
Matcher m = p.matcher(sb);
int count = 0;
while(m.find()){
count++;
}
System.out.println(count);
}
}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭