java基础——统计某篇文章中每个单词出现的次数

java基础——统计某篇文章中每个单词出现的次数

找到目标txt文件,

通过创建字符输入流对象,创建缓冲流,

依次读取每行字符,

追加至整篇文章字符,

通过正则表达式(一个或多个非单词字符)分隔,得到独立的单词,

遍历字符串数组,

将单词作为Map(无须不重复)的key,出现的次数作为Map的值。

如果map中不包含此单词,其value为1,否则value+1。通

过将map转化为set集合输出key-->map.get(key)。

1.创建工具类(包括读取文件,创建输入流,读取文章等方法)

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CountWordsUtil {

    //找到目标文件,创建字符输入流对象,
    public static Reader findFile(){
        File f=new File("E:/xn/The Old Man and the Sea .txt");
        Reader in=null;
        try{
            in=new FileReader(f);
        }catch(IOException e){
            e.printStackTrace();
        }
        return in;
    }
    //缓存流
    public static BufferedReader inputPipe(Reader in){
        BufferedReader br=null;
        br=new BufferedReader(in);
        return br;
    }
    //读取文章内容
    public static String readAll(BufferedReader br,Reader in){
        String str;
        Map map=new HashMap<>();
        StringBuilder words=null;
        String allwords=null;
        try {
            StringBuilder sb = new StringBuilder();
            while ((str = br.readLine()) != null) {

                words = sb.append(str);
                allwords=sb.toString();
            }
            br.close();
            in.close();
            }catch(IOException e){
                e.printStackTrace();
            }

            return allwords;
    }
    //将文章以非单词字符隔开,并存储在map键值对中,并输出
    public static void spiltAndCount(String allwords, Map map) {
        String regex = "\\W+";/*/[\s]|[ ]|[,]|[.]|[“”]|[?]|[  ]*/
        String[] words = allwords.split(regex);
        for (int i = 0; i < words.length; i++) {
            if (map.containsKey(words[i])) {
                map.put(words[i], map.get(words[i])+1);
            } else {
                map.put(words[i], 1);
            }
        }
        Set keys = map.keySet();

        for (String key : keys) {
            System.out.println(key + "---->" + map.get(key));
        }
    }

}

2.测试主类(读取文件)

import java.io.BufferedReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

public class ReadFile {
    public static void main(String[] args) {
        long  star =System.currentTimeMillis();

        Map map=new HashMap<>();
        Reader in= CountWordsUtil.findFile();
        BufferedReader br=CountWordsUtil.inputPipe(in);
        String allwords= CountWordsUtil.readAll(br,in);
        CountWordsUtil.spiltAndCount(allwords, map);
        long  end=System.currentTimeMillis();

        System.out.println("运行时间是:"+(end-star));
    }
}

3.写出文件

import java.io.*;

public class WriterFile {
    public static void main(String[] args){
        File f=new File("E:output.txt");
        try{
            Writer out=new FileWriter(f);

            Reader in= CountWordsUtil.findFile();
            BufferedReader br=CountWordsUtil.inputPipe(in);
            String allwords= CountWordsUtil.readAll(br,in);
            out.write(allwords,0,allwords.length());
            out.close();

        }catch(IOException e){
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(java基础——统计某篇文章中每个单词出现的次数)