欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Java_功能實(shí)現_WordCount
代碼:
package com.test.wordcount;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class WordCount {
    private static String path_src = "/home/CORPUSERS/xp009835/shenma.txt";
    private static String path_result = "/home/CORPUSERS/xp009835/shenma_result.txt";
    private static BufferedReader br = null;
    private static BufferedWriter bw = null;
    private static String line_current = null;
    private static String[] words = null;
    private static List<String> word_list = new ArrayList<String>();

    public static void main(String[] args) {
        File file = new File(path_src);
        if (!file.exists()) {
            System.out.println("file " + file + " is not existed, exit");
            return;
        }

        try {
            br = new BufferedReader(new FileReader(file.getPath()));
            line_current = br.readLine();
            while (line_current != null) {
                words = line_current.split(" |,|\\.");
                for (String s : words) {
                    if (!s.equals(""))
                        word_list.add(s);
                }

                line_current = br.readLine();
            }

            for (String temp : word_list) {
                System.out.println(temp);
            }

            // HashSet
            Set<String> hashSet = new HashSet<String>(word_list);
            for (String str : hashSet) {
                System.out.println("word: " + str +
                        ", occur times: " + Collections.frequency(word_list, str));
            }

            // HashMap
            Map<String, Integer> hashMap = new HashMap<String, Integer>();
            for (String temp : word_list) {
                Integer count = hashMap.get(temp);
                hashMap.put(temp, (count == null) ? 1 : count + 1);
            }

            // TreeMap
            TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>(
                    hashMap);

            // Record result to another file
            printMap(treeMap);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeInputStream(br);
            closeOutputStream(bw);
        }
    }

    public static void printMap(Map<String, Integer> map) throws IOException {

        bw = new BufferedWriter(new FileWriter(path_result));

        Set<String> keys = map.keySet();
        for (String s : keys) {
            System.out.println("word: " + s +
                    ", times: " + map.get(s));
            writeResult("word: " + s +
                    ", times: " + map.get(s));
        }
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("word: " + entry.getKey() + ", number : "
                    + entry.getValue());
            writeResult("word: " + entry.getKey() + ", number : "
                    + entry.getValue());
        }

    }

    public static void writeResult(String line) throws IOException {

        try {
            if (bw != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            closeOutputStream(bw);
        }
    }

    public static void closeOutputStream(BufferedWriter writer) {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void closeInputStream(BufferedReader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

shenma.txt文件內容:
ni hao ma, ni
aaa bb mam ma
li hao yu
luan tao, very good
luan luan ni is a bad man.

輸出:
word: a, times: 1
word: aaa, times: 1
word: bad, times: 1
word: bb, times: 1
word: good, times: 1
word: hao, times: 2
word: is, times: 1
word: li, times: 1
word: luan, times: 3
word: ma, times: 2
word: mam, times: 1
word: man, times: 1
word: ni, times: 3
word: tao, times: 1
word: very, times: 1
word: yu, times: 1

 

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
java TXT工具之去掉空格
靜態(tài)HTML生成模式代碼
讀取和處理csv文件為sql文件
Java 用Freemarker完美導出word文檔(帶圖片)
bootstrap-treeview demo (樹(shù)形展示與后臺數據交互)
Java 讀取網(wǎng)頁(yè)內容,并生成靜態(tài)頁(yè)面的方法
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久