从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序

1,张三,28

2,李四,35

3,张三,28

4,王五,35

5,张三,28

6,李四,35

7,赵六,28

8,田七,35

package com.swift;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument; public class IO_Sort_Select { public static void main(String[] args) throws IOException {
/*
* 从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序
*/
File file_s = new File("e:\\neck\\data.docx");
File dir = new File("e:\\neck");
if (!dir.exists() && dir.isDirectory()) {
System.out.println("目录不存在,即将创建...");
dir.mkdirs();
}
File file_t = new File(dir, "data.txt"); List<String> list = new ArrayList<String>();
String text = readFromDocx(file_s);
String[] hang = text.split("\\n");
for (int i = 0; i < hang.length; i++) {
String[] lie = hang[i].split("\\,");
for (int j = 0; j < lie.length; j++) {
if (j == 1) {
list.add(lie[j]);
}
} }
printList(list); Map<String, Integer> map = new TreeMap<String, Integer>();
for (String str : list) {
map.put(str, 0);
}
for(String str:list) {
if(map.containsKey(str)) {
int num=map.get(str);
num++;
map.remove(str);
map.put(str, num);
}
}
List<Entry<String,Integer>> listMap=new ArrayList<Entry<String,Integer>>(map.entrySet());
Collections.sort(listMap, new Comparator<Entry<String,Integer>>(){ @Override
public int compare(Entry<String, Integer> arg0, Entry<String, Integer> arg1) {
int num=arg1.getValue()-arg0.getValue();
return num==0?arg0.getKey().compareTo(arg1.getKey()):num;
}
}); for(Entry<String, Integer> entry:listMap) {
System.out.println("重复的姓名是 :"+entry.getKey()+" 重复的次数是:"+entry.getValue());
}
} private static void printList(List<String> list) {
for (String str : list) {
System.out.println(str);
}
} public static String readFromDocx(File file) throws IOException { FileInputStream fis = new FileInputStream(file);
XWPFDocument docx = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
String text = extractor.getText();
return text; } }

上面代码读取的是docx的word文件,直接读取会出现乱码,因word中不仅有文本还有压缩的其他属性等内容,所以要使用poi的jar包进行解析

解析的jar包如下图

这个应该比较全面了,也可以解析excel操作或写入表格等

下载地址:

链接: https://pan.baidu.com/s/1htYPKLA 密码: e36e

下面是读取doc的方法

package com.swift;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap; import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument; public class IO_Sort_Select { public static void main(String[] args) throws IOException {
/*
* 从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序
*/
File file_s = new File("e:\\neck\\data.doc");
File dir = new File("e:\\neck");
if (!dir.exists() && dir.isDirectory()) {
System.out.println("目录不存在,即将创建...");
dir.mkdirs();
} List<String> list = new ArrayList<String>();
String text = readFromDoc(file_s);
String[] hang = text.split("\\r");
for (int i = 0; i < hang.length; i++) {
System.out.println(hang.length);
System.out.println(hang[i]);
String[] lie = hang[i].split("\\,");
for (int j = 0; j < lie.length; j++) {
if (j == 1) {
list.add(lie[j]);
}
}
}
printList(list); Map<String, Integer> map = new TreeMap<String, Integer>();
for (String str : list) {
map.put(str, 0);
}
for(String str:list) {
if(map.containsKey(str)) {
int num=map.get(str);
num++;
map.remove(str);
map.put(str, num);
}
}
List<Entry<String,Integer>> listMap=new ArrayList<Entry<String,Integer>>(map.entrySet());
Collections.sort(listMap, new Comparator<Entry<String,Integer>>(){ @Override
public int compare(Entry<String, Integer> arg0, Entry<String, Integer> arg1) {
int num=arg1.getValue()-arg0.getValue();
return num==0?arg0.getKey().compareTo(arg1.getKey()):num;
}
}); for(Entry<String, Integer> entry:listMap) {
System.out.println("重复的姓名是 :"+entry.getKey()+" 重复的次数是:"+entry.getValue());
}
} private static void printList(List<String> list) {
for (String str : list) {
System.out.println(str);
}
} public static String readFromDocx(File file) throws IOException { FileInputStream fis = new FileInputStream(file);
XWPFDocument docx = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(docx);
String text = extractor.getText();
return text;
}
public static String readFromDoc(File file) throws IOException { FileInputStream fis = new FileInputStream(file);
HWPFDocument doc = new HWPFDocument(fis);
String text = doc.getDocumentText();
return text;
} }

doc读取出来的文本还不能用"\\n"进行行分割,用的是"\\r"才行,要不然只能分割出1行,这点注意,要不会觉得程序莫名其妙不安自己的思路走

java算法面试题:从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序 ;读取docx 读取doc 使用poi 相关jar包提集提供下载的更多相关文章

  1. Java算法面试题(史上最强、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  2. java算法面试题:有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数 按值的降序排序,如果值相同则按键值的字母顺序

    package com.swift; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; publi ...

  3. java算法面试题

    前言:线上面试题与大家分享,并记录求职道路的酸甜苦辣,特此留念. 李雷和韩梅梅坐前后排,上课想说话怕被老师发现,所以改为传小纸条.为了不被老师发现他们纸条上说的是啥,他们约定了如下方法传递信息:将26 ...

  4. java算法面试题:有数组a[n],用java代码将数组元素顺序颠倒

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.List; ...

  5. java算法面试题:设计一个快速排序。双路快速排序,简单易于理解。

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  6. java算法面试题:排序都有哪几种方法?请列举。用JAVA实现一个快速排序。选择冒泡快速集合至少4种方法排序

    package com.swift; import java.util.ArrayList; import java.util.Collections; import java.util.Compar ...

  7. java算法面试题:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个, 如“我ABC”,4,应该截取“我AB”,输入“我ABC汉DEF”,6,应该输出“我ABC”,而不是“我ABC+汉的半个”。

    package com.swift; import java.util.Scanner; public class Hanzi_jiequ { public static void main(Stri ...

  8. Java算法面试题:编写一个程序,将e:\neck目录下的所有.java文件复制到e:\jpg目录下,并将原来文件的扩展名从.java改为.jpg

    package com.swift; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; i ...

  9. java算法面试题:编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

随机推荐

  1. POJ1321-棋盘问题

    题目链接:点击打开链接 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和 ...

  2. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  3. 对sass通过compass进行编译

    1.创建一个Compass项目:compass create myproject 其中myproject是项目名称.2.编写scss文件.3.编译成css文件:①:Compass的编译命令是:comp ...

  4. (1009) HDU 6446 Tree and Permutation(规律+树上各个点的距离和)

    题意: 给一棵N个点的树,对应于一个长为N的全排列,对于排列的每个相邻数字a和b,他们的贡献是对应树上顶点a和b的路径长,求所有排列的贡献和. 分析: 经过简单的分析可以得知,全部的贡献其实相当与(这 ...

  5. 配置文件中取值: spring配置文件中util:properties和context:property-placeholder

    转载大神 https://blog.csdn.net/n447194252/article/details/77498916 util:properties和context:property-plac ...

  6. LeetCode 128 Longest Consecutive Sequence 一个无序整数数组中找到最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence.Fo ...

  7. Windows安全认证是如何进行的?[Kerberos篇]

    最近一段时间都在折腾安全(Security)方面的东西,比如Windows认证.非对称加密.数字证书.数字签名.TLS/SSL.WS-Security等.如果时间允许,我很乐意写一系列的文章与广大网友 ...

  8. 整理:sql server 中sql语句执行顺序

    SQL Server 查询处理中的各个阶段(SQL执行顺序) SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是 ...

  9. json解析数组类型的数据

    //微信里一个检测是否有发送模版消息的权限的方法//此处的openid代表的微信用户openid,templateId代表的是模版消息idpublic boolean checkIsSendTempM ...

  10. Django---ORM简介丶单表操作丶增删改查

    一丶ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...