1 package pingbi;
 2 
 3 /**
 4  * 将txt文本读入导入到set中
 5  * 问题:
 6  * 第一个地方有会多一个 ?--解决问题很简单,但不知道问题的原因
 7  */
 8 import java.io.BufferedReader;
 9 import java.io.File;
10 import java.io.FileInputStream;
11 import java.io.InputStreamReader;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15 
16 public class MyTest {
17     // private static String ENCODING = "gbk";
18     private static String ENCODING = "UTF-8";
19     
20     public static void main(String[] args) throws Exception {
21         Set<String> set = readKeyWord();
22 
23         Iterator<String> it = set.iterator();
24         while (it.hasNext()) {
25             String str = it.next();
26             System.out.println(str);
27         }
28         System.out.println(set.size());
29     }
30 
31     public static Set<String> readKeyWord() throws Exception {
32         Set<String> set = null;
33         
34         String filepath = "E:\\pingbi\\key.txt";
35         File file = new File(filepath); // 读取文件
36         //String filename = "key.txt";
37         
38         InputStreamReader read = new InputStreamReader(new FileInputStream(filepath), ENCODING);
39         try {
40             if (file.isFile() && file.exists()) {// 文件流是否存在
41                 set = new HashSet<String>();
42                 BufferedReader bufferedReader = new BufferedReader(read);
43                 String txt;
44                 while ((txt = bufferedReader.readLine()) != null) {//读取文件,将文件内容放入到set中
45                     set.add(txt);
46                 }
47             } else {//不存在抛出异常信息
48                 throw new Exception("敏感词库文件不存在");
49             }
50         } catch (Exception e) {
51             throw e;
52         } finally {
53             read.close();//关闭文件流
54         }
55         return set;
56     }
57 }