Java實(shí)現(xiàn)統(tǒng)計(jì)文檔中關(guān)鍵字出現(xiàn)的次數(shù)
該代碼簡(jiǎn)易實(shí)現(xiàn)了獲取URL地址后對(duì)文檔進(jìn)行關(guān)鍵字統(tǒng)計(jì)的功能。具體的自己看吧
1.實(shí)現(xiàn)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; //獲取用戶選擇的關(guān)鍵詞
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","輸入U(xiǎn)RL地址",JOptionPane.PLAIN_MESSAGE);
String urlAddr=getUserURL.substring(getUserURL.lastIndexOf("/"));
copyfile=new File("D:/"+urlAddr);
getUserKeyWords=JOptionPane.showInputDialog(null,"關(guān)鍵字查詢:\n","關(guān)鍵字",JOptionPane.PLAIN_MESSAGE);
//URL url=new URL("http://news.cctv.com/2019/06/19/ARTIhqziOpWz2COTyHFW063b190619.shtml"); //獲取URL地址
URL url=new URL(getUserURL); //獲取URL地址
HttpURLConnection urlC=(HttpURLConnection)url.openConnection(); //由URL獲取URLConnection對(duì)象
in=urlC.getInputStream(); //獲取urlC的輸入流
br=new BufferedReader(new InputStreamReader(in,"UTF-8")); //將url默認(rèn)的字節(jié)流轉(zhuǎn)成字符流,并以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);
} //程序到這就結(jié)束了 ,下面是不同方法實(shí)現(xiàn)對(duì)html的剔除功能,可以忽略
//從html中提取純文本 ,這部分其實(shí)沒什么用,最開始想截取html中的字符串,后面檢查也沒啥用,就沒刪除,保留著
public static String Html2Text(String inputString) {
String htmlStr = inputString; // 含html標(biāo)簽的字符串
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的正則表達(dá)式{或<script[^>]*?>[\\s\\S]*?<\\/script>
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定義style的正則表達(dá)式{或<style[^>]*?>[\\s\\S]*?<\\/style>
String regEx_html = "<[^>]+>"; // 定義HTML標(biāo)簽的正則表達(dá)式
p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); // 過濾script標(biāo)簽
p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); // 過濾style標(biāo)簽
p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); // 過濾html標(biāo)簽
textStr = htmlStr;
} catch (Exception e) {System.err.println("Html2Text: " + e.getMessage()); }
//剔除空格行
textStr=textStr.replaceAll("[ ]+", " ");
textStr=textStr.replaceAll("(?m)^\\s*$(\\n|\\r\\n)", "");
return textStr;// 返回文本字符串
}
/*//從html中提取純文本,這個(gè)部分是簡(jiǎn)易實(shí)現(xiàn)html剔除,只能部分篩選
public static String stripHT(String strHtml){
String txtcontent = strHtml.replaceAll("</?[^>]+>", ""); //剔出<html>的標(biāo)簽
txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");//去除字符串中的空格,回車,換行符,制表符
return txtcontent;
}*/
/* //這個(gè)是利用java自帶的類實(shí)現(xiàn)html剔除功能,基本上沒怎么用,這部分可以忽略
import java.io.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;
public class Html2Text extends HTMLEditorKit.ParserCallback {
StringBuffer s;
public Html2Text() {}
public void parse(Reader in) throws IOException {
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
// the third parameter is TRUE to ignore charset directive
delegator.parse(in, this, Boolean.TRUE);
}
public void handleText(char[] text, int pos) {
s.append(text);
}
public String getText() {
return s.toString();
}
public static void main (String[] args) {
try {
// the HTML to convert
//Reader in=new StringReader("string");
FileReader in = new FileReader("java-new.html");
Html2Text parser = new Html2Text();
parser.parse(in);
in.close();
System.out.println(parser.getText());
}
catch (Exception e) {
e.printStackTrace();
}
}
}*/
}2.實(shí)現(xiàn)關(guān)鍵詞在文檔的查詢功能
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import javax.swing.UIManager;
import javax.swing.*;
import java.awt.*;
import javax.swing.plaf.FontUIResource;
/**
* 對(duì)文本文件的關(guān)鍵詞進(jìn)行搜索
*/
public class TextFileSearch {
TestURL tt;
public void SearchKeyword(File file,String keyword) {
//參數(shù)校驗(yàn)
verifyParam(file, keyword);
//行讀取
LineNumberReader lineReader = null;
try {
lineReader = new LineNumberReader(new FileReader(file));
String readLine = null;
int times = 0;//出現(xiàn)的次數(shù)
while((readLine =lineReader.readLine()) != null){
//判斷每一行中,出現(xiàn)關(guān)鍵詞的次數(shù)
int index = 0; //獲得readLine的對(duì)象值
int next = 0; //定義開始查找關(guān)鍵字的序列號(hào)
//int times = 0;//出現(xiàn)的次數(shù)
//判斷次數(shù)
while((index = readLine.indexOf(keyword,next)) != -1) { //從每行的第0個(gè)索引開始遍歷關(guān)鍵字
next = index + keyword.length(); //下一次的遍歷序號(hào)為序列號(hào)+關(guān)鍵字長度
times++;//次數(shù)加1
}
/*if(times > 0) {
//System.out.println("第"+ lineReader.getLineNumber() +"行" + "出現(xiàn) "+keyword+" 次數(shù): "+times);
}*/
}
if (times>0)
{
UIManager.put("JOptionPane.messageFont",new FontUIResource(new Font("宋體",Font.BOLD,20)));
JOptionPane.showMessageDialog(null,"關(guān)鍵字"+"@"+tt.getUserKeyWords+"@"+"共有"+times+"個(gè)");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉流
close(lineReader);
}
}
/**
* 參數(shù)校驗(yàn)
*/
private void verifyParam(File file, String keyword) {
//對(duì)參數(shù)進(jìn)行校驗(yàn)證
if(file == null ){
throw new NullPointerException("the file is null");
}
if(keyword == null || keyword.trim().equals("")){
throw new NullPointerException("the keyword is null or \"\" ");
}
if(!file.exists()) {
throw new RuntimeException("the file is not exists");
}
//非目錄
if(file.isDirectory()){
throw new RuntimeException("the file is a directory,not a file");
}
//可讀取
if(!file.canRead()) {
throw new RuntimeException("the file can't read");
}
}
/**
* 關(guān)閉流
*/
private void close(Closeable able){
if(able != null){
try {
able.close();
} catch (IOException e) {
e.printStackTrace();
able = null;
}
}
}
}3.顯示效果
URL地址獲取效果圖

關(guān)鍵字查詢界面

查詢后效果圖

到此這篇關(guān)于Java實(shí)現(xiàn)統(tǒng)計(jì)文檔中關(guān)鍵字出現(xiàn)的次數(shù)的文章就介紹到這了,更多相關(guān)Java關(guān)鍵字次數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于JavaScript動(dòng)態(tài)規(guī)劃編寫一個(gè)益智小游戲
最近在學(xué)習(xí)動(dòng)態(tài)規(guī)劃相關(guān)的知識(shí),所以本文將利用動(dòng)態(tài)規(guī)劃編寫一個(gè)簡(jiǎn)單的益智小游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-06-06
淺談使用Java Web獲取客戶端真實(shí)IP的方法示例詳解
這篇文章主要介紹了使用Java-Web獲取客戶端真實(shí)IP的方法示例詳解。一般我們無法獲取客戶端真實(shí)IP,原因是:當(dāng)我們通過request獲取客戶端IP時(shí),自身服務(wù)器通常會(huì)為了保護(hù)信息或者負(fù)載均衡的目的,對(duì)自身服務(wù)器做反向代理。對(duì)此感興趣可以了解一下2020-07-07
java實(shí)現(xiàn)網(wǎng)上購物車程序
這篇文章主要介紹了java實(shí)現(xiàn)網(wǎng)上購物車程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
SpringSecurity實(shí)現(xiàn)前后端分離登錄token認(rèn)證詳解
目前市面上比較流行的權(quán)限框架主要實(shí)Shiro和Spring Security,這兩個(gè)框架各自側(cè)重點(diǎn)不同,各有各的優(yōu)劣,本文將給大家詳細(xì)介紹SpringSecurity如何實(shí)現(xiàn)前后端分離登錄token認(rèn)證2023-06-06
JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析
這篇文章主要介紹了JVM內(nèi)存結(jié)構(gòu)相關(guān)知識(shí)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Spring加載XSD文件發(fā)生錯(cuò)誤的解決方法
這篇文章主要介紹了Spring加載XSD文件發(fā)生錯(cuò)誤的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
IDEA遇到Internal error. Please refer to http://jb. gg/ide/crit
這篇文章主要介紹了IDEA遇到Internal error. Please refer to http://jb. gg/ide/critical-startup-errors的問題及解決辦法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-08-08

