Ai
1 Star 0 Fork 85

宋佳豪/Project-Java

forked from 有朝一日/Project-Java 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
WordCount.java 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
song 提交于 2021-04-02 11:31 +08:00 . 完成
import java.io.*;
import java.util.*;
public class WordCount {
private static final File ROOT_File =new File("D:\\桌面");
private static int count = 0;
private static Map<String,Integer> wordCount = new HashMap<>();
public static void main(String [] args) throws Exception{
String intputFileName = args[0];
String outputFileName = args[1];
File inputFile = new File(ROOT_File,intputFileName);
File outputFile = new File(ROOT_File,outputFileName);
// 判断是否存在:
if(inputFile.exists()){
doCheck(inputFile);
}else{
throw new RuntimeException("error");
}
PrintStream stream = new PrintStream(new FileOutputStream(outputFile));
System.setOut(stream);
show();
System.out.println("单词数:"+obtainTotalWords());
System.out.println("行数:"+count);
System.out.println("字符数:"+(inputFile.length()));
}
public static void doCheck(File inputFile) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
String line = null;
while(null!=(line=br.readLine())){
incrLine();
// 分析每一行。
analysis(line);
}
}
public static void show(){
Set<Map.Entry<String, Integer>> entries = wordCount.entrySet();
// 排序
ArrayList<String> words = new ArrayList<>();
for (Map.Entry<String, Integer> entry : entries) {
words.add(entry.getValue()+"#"+entry.getKey());
}
// 排序
Collections.sort(words);
words.forEach(obj->{
String[] split = obj.split("#");
String str = split[1]+": "+split[0];
System.out.println(str);
});
}
public static void incrLine(){
// 行数加1
count++;
}
//总单词数
public static long obtainTotalWords(){
long sum = 0;
Set<Map.Entry<String, Integer>> entries = wordCount.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
sum+=entry.getValue();
}
return sum;
}
// 得到每一个单词以及次数, 并且记录到Map集合中
public static void analysis(String line){
String [] words = line.split(" ");
for(int i=0;i<words.length;i++){
String word = words[i].trim();
word = word.toLowerCase();
word = word.contains(",")||word.contains(".")?word.substring(0,word.length()-1):word;
if(word.length()>=4&&isWord(word.substring(0,4))){
if(wordCount.containsKey(word)){
Integer count = wordCount.get(word);
count++;
wordCount.put(word,count);
}else{
wordCount.put(word,1);
}
}
}
}
public static boolean isWord(String word){
for(int i=0;i<word.length();i++){
if(word.charAt(i)>=97 && word.charAt(i)<=122){
continue;
}else{
return false;
}
}
return true;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Songjhao/project-java.git
git@gitee.com:Songjhao/project-java.git
Songjhao
project-java
Project-Java
master

搜索帮助