java中,给定一个文本,统计其中的单词个数,要求以单词在文本中出现的先后顺序输出

2024-12-11 21:40:22
推荐回答(2个)
回答1:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordCount {
    public static void main(String[] args) throws Exception {
        File file = new File("1.txt");
        PrintWriter pw = new PrintWriter(file);
        pw.println("hello world, hello java.");
        pw.println("hello program, hello java.");
        pw.close();
        printWords(file);
    }
    public static void printWords(File file) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(file));
        Pattern pattern = Pattern.compile("[A-Za-z]+");
        Map map = new LinkedHashMap<>();
        while (br.ready()) {
            Matcher matcher = pattern.matcher(br.readLine());
            while (matcher.find()) {
                String word = matcher.group();
                if (!map.containsKey(word)) {
                    map.put(word, 0);
                }
                map.put(word, map.get(word) + 1);
            }
        }
        br.close();
        for (String word : map.keySet()) {
            System.out.println(word + " : " + map.get(word));
        }
    }
}

回答2:

不就是复制文本么?以空格切割文本,以字节流输出就行了