要求: 
  任意选择一个文本文件,分析该文件,如果存在敏感词汇(这些词汇保存在sentive.txt文件中,每个词占一行),报告每个词出现的次数。 
  简单一点就好,谢谢!!

解决方案 »

  1.   

    给个思路吧 学习JAVA IO 那一章节!!呵呵
      

  2.   

    这个会些IO的话,应该不是什么难事阿.!  LZ对着API写吧
      

  3.   

    sentive.txt是字典表,用文件流读入内存存放在一个list里,个人认为放在map里可能更好,Map<String,Integer> 保存<敏感词,1>,这样在遍历的时候就无需循环了。然后读取要验证的文本文件,循环中map.get()敏感词,返回null不管,返回1就用计数器++
      

  4.   

    忘了你这个还有个难点,不知道词的长度,那就这样做,敏感词字典还是放在list里,按行读取要分析的文本文件,每行文本用string.contains()去循环判断每个敏感词是否存在,然后计数
      

  5.   

    思路:首先从配置读取敏感词清单放到内存中,然后扫描指定的文本,逐个敏感词进行统计,统计结果汇总到内存中,最后输出统计结果。代码如下:package demo;import java.io.BufferedReader;
    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);
    }
    }}
      

  6.   

    读取文件置入StringBuffer中,再把StringBuffer内容取出换为String类型,使用indexOf()方法,或者equals()方法来判断是否为敏感词。
      

  7.   

    回复8楼:
           运行之后显示系统找不到指定文件<f:/mcb/docs/Sensitive.txt>,怎么回事呀?
      

  8.   

    可不可以写一个不用hashmap的?
      

  9.   

    import java.io.*;
    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();
        }
    }都有啥问题呀?