日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现读取txt文本 统计文本单词出现次数

發布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现读取txt文本 统计文本单词出现次数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//介紹:
//InputStream 是所有字節輸入流的超類,一般使用它的子類:FileInputStream等,它能輸出字節流;其他地方往應用程序輸入數據,也就是從其他位置讀取數據在應用程序中;
//InputStreamReader是字節流與字符流之間的橋梁,能將字節流輸出為字符流,并且能為字節流指定字符集,可輸出一個個的字符。
//FileInputStream 繼承于InputStream 用于讀取本地文件中的字節數據,屬于文件操作。
//BufferedReader :提供通用的緩沖方式文本讀取,readLine讀取一個文本行,從字符輸入流中讀取文本,緩沖各個字符,從而提供字符、數組和行的高效讀取。

//File 文件類 文件可執行的操作有創建、刪除、查看文件是否存在、查看文件是否包含內容

我將讀取txt文本內容并統計每個單詞出現次數 并將結果排序 分成四個小問題

1)讀取文本內容

2)將文本內容分割成一個個字符串數組(單詞) 并統計每個單詞出現的次數

3)對統計結果進行排序

4)將結果輸出

完整的java代碼如下:

public class TxtFile {

? ? private String path;

? ? public TxtFile( ) {

? ? }

? ? /**
? ? ?* 用文檔的路徑構造一個txtFile(txt文件)類
? ? ?* @param _path 文檔的路徑
? ? ?*/
? ? public TxtFile( String _path) {
? ? ? ? this.path = _path ;
? ? }

? ? public void setPath(String path) {
? ? ? ? this.path = path;
? ? }

? ? public String getPath() {
? ? ? ? return path;
? ? }

? ? /**
? ? ?* 功能:從txt文本中讀取所有行內容并以一個字符串的形式將文本內容返回
? ? ?* @return 字符串形式的文本內容
? ? ?* @throws IOException
? ? ?*/
? ? public String readWords() throws IOException {

//將文件中內容讀取到應用程序中 用的是輸入流(對應用程序而言)
// 也就是將文件中的字節轉換成字符以緩沖方式進行內容的讀取 利用輸入流讀取類讀取內容
// 輸入流讀取創建的關鍵在于讀取的是那個文件 文件的編碼為何種
//使用緩沖方式利用輸入流讀取對象進行讀取多行讀取內容
? ? ? ? String encoding = "GBK"; // 字符編碼(可解決中文亂碼問題 )
? ? ? ? File file = new File(this.path);

? ? ? ? String words = "";

? ? ? ? if (file.isFile() && file.exists()) {
? ? ? ? ? ? //輸入讀取流
? ? ? ? ? ? InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);

? ? ? ? ? ? //通過默認的帶有Buffer的字節輸入流與字符輸入流來多行讀取txt文本。
? ? ? ? ? ? BufferedReader bufferedReader = new BufferedReader(read);
? ? ? ? ? ? String lineTXT = null;

? ? ? ? ? ? //一行行讀取 直到內容為空 因為內容以字符串的形式返回并用空格進行分割 所以在行末尾加上了空格
? ? ? ? ? ? while ((lineTXT = bufferedReader.readLine()) != null) {
? ? ? ? ? ? ? ? words += lineTXT.toString();
? ? ? ? ? ? ? ? words += " ";
? ? ? ? ? ? }
? ? ? ? ? ? read.close();

? ? ? ? }
? ? ? ? return words;
? ? }

? ? /**
? ? ?* 將文本的內容分割成一個個單詞 并統計每個單詞出現的次數
? ? ?* @param text 字符串形式的文本內容
? ? ?* @return 返回鍵為單詞 值為單詞出現的個數的 TreeMap類型的 單詞統計情況
? ? ?*/
? ? public Map<String,Integer> countAWord(String text)
? ? {
? ? ? ? Map<String,Integer> map = new TreeMap<String,Integer>();
? ? ? ? String[] str = text.split(" ");
? ? ? ? for(int i=0;i<str.length; i++)
? ? ? ? {
? ? ? ? ? ? int num =1;
? ? ? ? ? ? for(int j=0;j<str.length ;j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(j!=i && str[i].equals(str[j])) {
? ? ? ? ? ? ? ? ? ? num++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? map.put(str[i],num) ;
? ? ? ? }
? ? ? ? return map ;
? ? }

? ? /**
? ? ?* 功能:將單詞出現次數情況按照出現次數進行降序排序
? ? ?* @param map 文本內容中每個單詞出現個數的統計情況 存儲格式為TreeMap 鍵為單詞 值為單詞出現個數
? ? ?* @return 經過排序的單詞統計情況 存儲格式為list
? ? ?*/
? ? public List<Map.Entry<String, Integer>> wordsSort(Map<String,Integer> map)
? ? {
? ? ? ? //Map是以Map.Entry<鍵,值>的形式進行存儲 將map裝載到list中
? ? ? ? List<Map.Entry<String,Integer>> words = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());

? ? ? ? //重寫sort方法 比較器將兩個相鄰的list元素按照list中的map的值進行排序 -降序 和冒泡排序相似 返回排序后的結果
? ? ? ? Collections.sort(words, new Comparator<Map.Entry<String, Integer>>() {

? ? ? ? ? ? public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
? ? ? ? ? ? ? ? //return (o2.getValue() - o1.getValue());
? ? ? ? ? ? ? ? return -(o1.getValue()).toString().compareTo(o2.getValue().toString());
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? return words;
? ? }

? ? /**
? ? ?* 將單詞出現情況輸出至控制臺
? ? ?* @param words 排序后的單詞出現的統計情況 存儲格式為list中元素時map
? ? ?*/
? ? public void print(List<Map.Entry<String, Integer>> words)
? ? {
? ? ? ? for (int i = 0; i < words.size(); i++) {

? ? ? ? ? ? System.out.printf("%.8s %d\n",words.get(i).getKey() ,words.get(i).getValue() );
? ? ? ? }
? ? }

}

主函數調用:

? ? ? ? TxtFile txtFile = new TxtFile("f:/data.txt") ;

? ? ? ? String words = txtFile.readWords() ;
? ? ? ? Map<String,Integer> map =null ;
? ? ? ? map = txtFile.countAWord(words) ;

? ? ? ? List<Map.Entry<String, Integer>> list = null;
? ? ? ? list = txtFile.wordsSort(map) ;
? ? ? ? txtFile.print(list) ;

總結

以上是生活随笔為你收集整理的实现读取txt文本 统计文本单词出现次数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。