Java实现统计文档中关键字出现的次数(统计字符串出现的次数 java)

网友投稿 378 2022-07-30


目录1.实现URL文档的拷贝2.实现关键词在文档的查询功能3.显示效果

该代码简易实现了获取URL地址后对文档进行关键字统计的功能。具体的自己看吧

1.实现URL文档的拷贝

import java.util.Scanner;

import java.util.regex.Pattern;

import java.net.*;

import java.io.*;

import javax.swing.*;

import javax.swing.UIManager;

import java.awt.*;

import javax.swing.plaf.FontUIResource;

public class TestURL {

static String getUserKeyWords=null; //获取用户选择的关键词

public static void main(String[] args) {

File copyfile=new File("D:/newTest.txt");

InputStream in=null;

BufferedReader br=null; //字符流写入

BufferedWriter out=null; //字符流写出

String urladdress=null; //获取用户输入的URL地址

try

{

UIManager.put("JOptionPane.messageFont",new FontUIResource(new Font("宋体",Font.BOLD,20)));

String getUserURL=JOptionPane.showInputDialog(null,"URL地址:\n","输入URL地址",JOptionPane.PLAIN_MESSAGE);

String urlAddr=getUserURL.substring(getUserURL.lastIndexOf("/"));

copyfile=new File("D:/"+urlAddr);

getUserKeyWords=JOptionPane.showInputDialog(null,"关键字查询:\n","关键字",JOptionPane.PLAIN_MESSAGE);

//URL url=new URL("http://news.cctv.com/2019/06/19/ARTIhqziOpWz2COTyHFW063b190619.shtml"); //获取URL地址

URL ukAfIVrl=new URL(getUserURL); //获取URL地址

HttpURLConnection urlC=(HttpURLConnection)url.openConnection(); //由URL获取URLConnection对象

in=urlC.getInputStream(); //获取urlC的输入流

br=new BufferedReader(new InputStreamReader(in,"UTF-8")); //将url默认的字节流转成字符流,并以UTF-8的格式写入文档

out=new BufferedWriter(new FileWriter(copyfile)); //将获取的信息写入到TestURL文档中

String length=null;

while ((length=br.readLine())!=null)

{

out.write(Html2Text(length));

out.newLine();

}

}

catch (Exception e)

{

e.getMessage();

}finally{

System.out.println("拷贝完成!");

try{

if (in!=null){in.close();}

if (out!=null){out.close();}

if (br!=null){br.close();}

}catch(Exception ee){

ee.getMessage();

}

}

TextFileSearch search = new TextFileSearch();

search.SearchKeyword(copyfile, getUserKeyWords);

} //程序到这就结束了 ,下面是不同方法实现对html的剔除功能,可以忽略

//从html中提取纯文本 ,这部分其实没什么用,最开始想截取html中的字符串,后面检查也没啥用,就没删除,保留着

public static String Html2Text(String inputString) {

String htmlStr = inputString; // 含html标签的字符串

String textStr = "";

java.util.regex.Pattern p_script;

java.util.regex.Matcher m_script;

java.util.regex.Pattern p_style;

java.util.regex.Matcher m_style;

java.util.regex.Pattern p_html;

java.util.regex.Matcher m_html;

try {

String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解析Idea为什么不推荐使用@Autowired进行Field注入
下一篇:Java实现简易拼图游戏的方法详解(java编写拼图游戏)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~