首页 > 编程 > Java > 正文

java将文件中的内容读取到HashSet中/内存中

2019-11-08 19:30:05
字体:
来源:转载
供稿:网友

java将文件读到hashSet/内存中

PRivate Set<String> readWordFile() {

Set<String> wordSet = null;// 要读取的文件路径,这里自行更改File file = new File("D:/workspace/SensitiveWordDeal/resources/censorwords.txt");try {// 读取文件输入流InputStreamReader read = new InputStreamReader(new FileInputStream(file), Encoding);// 文件是否是文件 和 是否存在if (file.isFile() && file.exists()) {wordSet = new HashSet<String>();//字符缓存输入流BufferedReader br = new BufferedReader(read);String txt = null;// 读取文件,将文件内容放入到set中while ((txt = br.readLine()) != null) {wordSet.add(txt);}br.close();}// 关闭文件流read.close();} catch (Exception e) {e.printStackTrace();}return wordSet;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表