最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)統(tǒng)計(jì)文檔中關(guān)鍵字出現(xiàn)的次數(shù)

 更新時(shí)間:2022年05月12日 09:31:35   作者:錯(cuò)過了時(shí)間  
這篇文章主要為大家分享了利用Java語言實(shí)現(xiàn)統(tǒng)計(jì)關(guān)鍵字在文檔中出現(xiàn)的次數(shù)的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

該代碼簡(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)文章

  • Java?ObjectMapper的使用和使用過程中遇到的問題

    Java?ObjectMapper的使用和使用過程中遇到的問題

    在Java開發(fā)中,ObjectMapper是Jackson庫的核心類,用于將Java對(duì)象序列化為JSON字符串,或者將JSON字符串反序列化為Java對(duì)象,這篇文章主要介紹了Java?ObjectMapper的使用和使用過程中遇到的問題,需要的朋友可以參考下
    2024-07-07
  • Java中ByteArrayOutputStream亂碼問題解決

    Java中ByteArrayOutputStream亂碼問題解決

    本文主要介紹了Java中ByteArrayOutputStream亂碼問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • java多線程三種上鎖方式小結(jié)

    java多線程三種上鎖方式小結(jié)

    本文主要介紹了java多線程三種上鎖方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • Java 文件上傳與路徑處理之Paths.get()、resolve()、transferTo()的用法詳解

    Java 文件上傳與路徑處理之Paths.get()、resolve()、transferTo()的用法詳解

    本文詳細(xì)解析了如何使用Java的Paths.get()、resolve()和Spring的transferTo()方法處理文件上傳功能,并給出了完整的代碼示例,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • 在Java中為日期增加一天的多種方法

    在Java中為日期增加一天的多種方法

    這篇文章主要給大家介紹了關(guān)于如何在Java中為日期增加一天的多種方法,在JAVA業(yè)務(wù)代碼中,經(jīng)常會(huì)遇到通過指定時(shí)間,增加指定天數(shù)的業(yè)務(wù)需求,需要的朋友可以參考下
    2023-07-07
  • spring-security關(guān)于hasRole的坑及解決

    spring-security關(guān)于hasRole的坑及解決

    這篇文章主要介紹了spring-security關(guān)于hasRole的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java使用Runtime執(zhí)行系統(tǒng)命令遇到的問題

    java使用Runtime執(zhí)行系統(tǒng)命令遇到的問題

    這篇文章主要介紹了java使用Runtime執(zhí)行系統(tǒng)命令遇到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 關(guān)于Java集合框架面試題(含答案)上

    關(guān)于Java集合框架面試題(含答案)上

    Java集合框架為Java編程語言的基礎(chǔ),也是Java面試中很重要的一個(gè)知識(shí)點(diǎn)。這里,我列出了一些關(guān)于Java集合的重要問題和答案。
    2015-12-12
  • 使用Feign調(diào)用注解組件(實(shí)現(xiàn)字段賦值功能)

    使用Feign調(diào)用注解組件(實(shí)現(xiàn)字段賦值功能)

    這篇文章主要介紹了使用Feign調(diào)用注解組件(實(shí)現(xiàn)字段賦值功能),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring Boot 緩存 Cache 入門詳解

    Spring Boot 緩存 Cache 入門詳解

    本文主要介紹SpringBoot緩存的入門知識(shí),包括緩存的必要性、常見的緩存策略、SpringCache的注解使用、SpringBoot與緩存的集成以及Ehcache和Redis的示例,同時(shí),還提供了緩存面試問題的思路和答案,幫助讀者更好地理解和掌握SpringBoot緩存的相關(guān)內(nèi)容,感興趣的朋友一起看看吧
    2025-03-03

最新評(píng)論

策勒县| 吴堡县| 玉田县| 台中市| 潮州市| 错那县| 色达县| 商城县| 兴国县| 余干县| 松溪县| 海盐县| 平安县| 桃源县| 塔河县| 石屏县| 营山县| 北流市| 吐鲁番市| 宁南县| 旌德县| 周至县| 绩溪县| 阿克| 定陶县| 台中市| 色达县| 得荣县| 泰兴市| 黄山市| 招远市| 武夷山市| 加查县| 名山县| 广宁县| 蓬溪县| 丁青县| 舞阳县| 古田县| 台北县| 济阳县|