要求:
任意选择一个文本文件,分析该文件,如果存在敏感词汇(这些词汇保存在sentive.txt文件中,每个词占一行),报告每个词出现的次数。
简单一点就好,谢谢!!
任意选择一个文本文件,分析该文件,如果存在敏感词汇(这些词汇保存在sentive.txt文件中,每个词占一行),报告每个词出现的次数。
简单一点就好,谢谢!!
解决方案 »
- 求教session的问题
- jsp连接数据库,上传头像如何实现?
- 请教:J2EE与J2ME哪个更有发展呀?
- Hibernate非主键列自增问题
- 新增记录时ActionForm中没有编辑的字段怎么保持null而不是""(空字符串)
- 小弟是在做linux集群公司工作的,用JAVA开发集群相关的软件,熟悉JSP,servlet,struts,javabean,linux,到杭州找JAVA工作一般有个多少月薪
- 哪个ejb容器对资源的要求比较低,且能集成到jb9中??
- 谁能帮我推荐一本关于struts的好书?
- 在线等待,JAVA如何播放多媒体文件?
- 关于weblogic中自带的ejb的问题
- The requested resource (Servlet action is not available) is not available.
- 小屁孩求教!
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;public class SensitiveCheck {
private final String config="f:/mcb/docs/Sensitive.txt";//敏感词清单,每个词占一行
HashMap<String, Integer> keywords;//保存解析结果,解析完成以后可以把结果输出到文本 /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SensitiveCheck sensitiveCheck=new SensitiveCheck();
sensitiveCheck.init();
sensitiveCheck.parseFile("f:/mcb/docs/t.txt");
sensitiveCheck.printResult();
}
//对指定的文件进行解析
public void parseFile(String targetFile){
File file=new File(targetFile);
StringBuffer buffer=new StringBuffer();
try {
BufferedReader bReader=new BufferedReader(new FileReader(file));
String temp=null;
while((temp=bReader.readLine())!=null){
buffer.append(temp);
}
String fileContent=buffer.toString();
if(keywords!=null){
for (Iterator<String> iterator = keywords.keySet().iterator(); iterator
.hasNext();) {
String key = (String) iterator.next();
int count=matchAll(fileContent,key,0);
keywords.put(key, count);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//利用递归查找文件中出现的指定敏感词的数量
public int matchAll(String content,String key,int count){
int index=content.indexOf(key);
if(index==-1){
return count;
}else{
count++;
content=content.substring(index+key.length());
return matchAll(content, key, count);
}
}
//初始化敏感词清单,放到Hashmap中,同时该hashmap也用来存放统计结果。统计完成后可以输出到文本
public void init() {
keywords=new HashMap<String, Integer>();
File file=new File(config);
try {
BufferedReader bReader=new BufferedReader(new FileReader(file));
String temp=null;
while((temp=bReader.readLine())!=null){
if(!keywords.containsKey(temp)){
keywords.put(temp.trim(), 0);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//打印统计结果
public void printResult() {
for (Iterator<String> iterator = keywords.keySet().iterator(); iterator.hasNext();) {
String type = (String) iterator.next();
int count=keywords.get(type);
System.out.println("关键字:"+type+",出现次数:"+count);
}
}}
运行之后显示系统找不到指定文件<f:/mcb/docs/Sensitive.txt>,怎么回事呀?
public class SensitiveCheck
{
File file=new File("/path/","Sensitive.txt");
public int i=0;
public String[] aline=null;public SensitiveCheck(){}
public void readFromSFile()throws IOException
{
FileReader fin=new FileReader(file);
BufferedReader bin=new BufferedReader(fin);
String str=null;
do
{
str=bin.readLine();
if(str!=null)
aline[i++]=str;
}while(str!=null);
bin.close();
fin.close();}public int[] count=new int[aline.length];
public void readFromTFile() throws IOException
{File file2=new File("/path/","t.txt");
FileReader fin=new FileReader(file2);
BufferedReader bin=new BufferedReader(fin);
String str=null;
for(i=0;i<aline.length;i++)
do
{
str=bin.readLine();
if(str!=null)
while(true)
{
int j=str.indexOf(aline[i]);
if(j!=-1)
{
count[i]+=1;
int k=aline[i].length();
str=str.substring(j+k+1);
}
else
{
break;
}
}
}while(str!=null);
bin.close();
fin.close();
}public void result()
{
for(i=0;i<aline.length;i++)
System.out.println("敏感词:"+aline[i]+"出现的次数 "+count[i]+"次");
}
public static void main(String[] args) throws IOException
{
SensitiveCheck sensitivecheck=new SensitiveCheck();
sensitivecheck.readFromSFile();
sensitivecheck.readFromTFile();
sensitivecheck.result();
}
}都有啥问题呀?