Java作业整理 敏感词汇
2015-07-07 15:58
661 查看
1. 敏感词汇分析(难)
要求:任意选择一个文本文件,分析该文件,如果存在敏感词汇(这些词汇保存在sentive.txt文件中,每个词占一行),报告每个词出现的次数。
要求:任意选择一个文本文件,分析该文件,如果存在敏感词汇(这些词汇保存在sentive.txt文件中,每个词占一行),报告每个词出现的次数。
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Scanner; public class sensitive_Vocabulary { Scanner input=new Scanner(System.in); public String[] ch; public void write(int number) throws Exception{ System.out.println("请输入你的敏感词汇:"); ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("sensitive.txt")); for(int i = 0; i < number;i++){ String str = input.nextLine(); output.writeUTF(str); } output.close(); } public void readObject(int number) throws Exception{ ObjectInputStream input = new ObjectInputStream(new FileInputStream("sensitive.txt")); ch = new String[number]; for(int j = 0; j < number; j++){ ch[j] = input.readUTF(); // System.out.println("敏感词汇为:"+ch[j]); } input.close(); } } import java.util.Scanner; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class read_File { public static void main(String[] args) throws Exception { Scanner input=new Scanner(System.in); System.out.println("请输入你所要输入的敏感词汇个数:"); int number=input.nextInt(); int[] array=new int[number]; for(int r=0;r<number;r++) array[r]=0; sensitive_Vocabulary test=new sensitive_Vocabulary(); test.write(number); test.readObject(number); try { StringBuffer sb= new StringBuffer(""); FileReader reader = new FileReader("G://test.txt"); BufferedReader br = new BufferedReader(reader); String str = null; String str_fir; while((str = br.readLine()) != null){ sb.append(str+"\n"); for(int k=0;k<number;k++){ str_fir=" "+test.ch[k]+" "; array[k] += countStr(str,str_fir); // System.out.println(array[k] +" " +str+"\n"+test.ch[k]); } } br.close(); reader.close(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } for(int j=0;j<number;j++) System.out.println("敏感词"+" "+test.ch[j]+" " +"有"+array[j] +"个"); } public static int countStr(String str1, String str2){ int counter=0; if (str1.indexOf(str2) == -1){ return 0; } while(str1.indexOf(str2)!=-1){ counter++; str1=str1.substring(str1.indexOf(str2)+str2.length()); } return counter; } }
相关文章推荐
- Spring, 基于自动代理实现aop
- spring mvc from表单上传后台实现方法(适用android/ios和jsp)
- JAVA多态学习1
- springmvc缓存
- java作业整理
- java细节总结
- springMVC注解方式+easyUI+MYSQL配置实例
- 通过ApplicationContextAware加载Spring上下文环境
- Java中堆内存和栈内存详解
- Eclipse常用快捷键
- java 对象与byte[]互转
- 多线程中synchronized作用
- myeclipse代码中出现 The word 'carousel' is not correctly spelled
- 堆排序(Java实现)
- myeclipse building workspace如何禁止及提高myeclipse速度
- java.security.AccessControlException: access denied (java.util.PropertyPermi
- 【Java】编写一个方法,实现基本的字符串压缩功能
- java线程同步锁synchronized的认识
- Spring2.5学习4.1_静态代理实现
- Spring2.5学习4.1_静态代理实现