zoukankan      html  css  js  c++  java
  • 读取txt内容放入set中

     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 }
  • 相关阅读:
    Javascript是单线程的深入分析
    非阻塞式JavaScript脚本介绍
    javascript 关于函数的返回值
    javascript运算符的优先级
    JavaScript 中的 this
    javascript中关于坐标 大小 的描述
    Javascript引擎单线程机制及setTimeout执行原理说明
    回车登录页面的问题
    有关架构的若干思考
    Bootstrap3 模态框 select2搜索框无法输入
  • 原文地址:https://www.cnblogs.com/dayuruozhi/p/6595794.html
Copyright © 2011-2022 走看看