急急急!!!!!!!!!!

asdaszxczxvcbvcb 2010-03-10 07:50:57
给定一个字符串(不包括回车换行之类的特殊字符),给定一个文本文件(可能含有汉字),如果某一行中包含该字符串就算一次,打印出该文件中总共出现多少次。
...全文
83 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
duiduiaa 2010-03-10
  • 打赏
  • 举报
回复
某一行中包含该字符串就算一次 不用拼写行与行的关系吧
duiduiaa 2010-03-10
  • 打赏
  • 举报
回复
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ming.txt")));
String data = null;
int m = 0;
while((data = br.readLine())!=null)
{
boolean b = data.matches("你的字符串的表达式");
if(b)
m++;
}
酷python 2010-03-10
  • 打赏
  • 举报
回复

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile{
public int getCount(String str,String filename){
int count=0;
File file=new File(filename);
BufferedReader reader=null;
String target="";
try {
reader=new BufferedReader(new FileReader(file));
String string="";
//每次读取一行
while((string=reader.readLine())!=null){
//把所有行连在一起,以防要查找的字符串因换行被截断
target=target+string;
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(target);
//如果文件中读取的字符串中有要计算的字符串,就替换,每次只替换一个
while(target.indexOf(str)!=-1){
target=target.replaceFirst(str, "try");
count++;
}
return count;
}

public static void main(String args[]){
ReadFile rf=new ReadFile();
//我试验的是一,文件里的内容就是你发帖的内容
System.out.println(rf.getCount("一", "C:/test.txt"));
}
}
hjh811 2010-03-10
  • 打赏
  • 举报
回复
lz有一帖又一帖发作业帖的嫌疑。。。鉴定完毕

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧