Java读取txt文件,并且对其文件内容进行统计排序

Posted 静谧星空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java读取txt文件,并且对其文件内容进行统计排序相关的知识,希望对你有一定的参考价值。

1、业务需求 : 根据使用量将下列数据进行排序 然后按照使用量的高低进行 数据展示

2、实现代码

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
/**
 * Java读取txt文件,并且对其文件内容进行统计排序
 * @author com
 *
 */
public class ClassInfoSort {

	static String str = null;

	static Map<String, Double> map1 = new HashMap<String, Double>(); // G
	static Map<String, Double> map2 = new HashMap<String, Double>(); // M
	static Map<String, Double> map3 = new HashMap<String, Double>(); // K
	static Map<String, Double> map4 = new HashMap<String, Double>(); // 0

	public static void main(String[] args) {
		read();
		output();
	}

	public static void output() {
		List<Map.Entry<String, Double>> list1 = sort(map1);
		List<Map.Entry<String, Double>> list2 = sort(map2);
		List<Map.Entry<String, Double>> list3 = sort(map3);
		//List<Map.Entry<String, Double>> list4 = sort(map4);
		for (Entry<String,Double> entry : list1) {
			System.out.println(entry.getKey()+" "+entry.getValue()+" G");
		}
		for (Entry<String,Double> entry : list2) {
			System.out.println(entry.getKey()+" "+entry.getValue()+" M");
		}
		for (int i=0;i<list3.size();i++) {
			System.out.println(list3.get(i).getKey()+" "+list3.get(i).getValue()+" K");
		}
		map4.forEach(new BiConsumer<String, Double>() {
			@Override
			public void accept(String t, Double u) {
				System.out.println(t+" "+u+" B");
			}
		});
	}
	/**
	 * map以value值降序排序,返回list<Map>
	 * @param map
	 * @return list
	 */
	public static List<Map.Entry<String, Double>> sort(Map<String,Double> map){
		List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String,Double>>(map.entrySet());
		list.sort(new Comparator<Map.Entry<String, Double>>() {
			@Override
			public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
				return o2.getValue().compareTo(o1.getValue());
			}
		});
		return list;
	} 

	/**
	 * 读取文件,并筛选出user用户和对应的文件大小
	 */
	public static void read() {
		File file = new File("作业1.txt");
		FileInputStream fin = null;
		BufferedReader bfr = null;
		try {
			fin = new FileInputStream(file);
			bfr = new BufferedReader(new InputStreamReader(fin));
			String str = null;
			String key = null;
			double value = 0D;
			int n, n1;
			while ((str = bfr.readLine()) != null) {
				if (str.startsWith("0")) { // 0的情况
					n = str.indexOf("/user/") + 6;
					key = str.substring(n);
					value = 0;
					map4.put(key, value);
				} else if (str.contains(" G")) { // G的情况
					n = str.indexOf("/user/") + 6;
					key = str.substring(n);
					n1 = str.indexOf(" G");
					value = Double.parseDouble(str.substring(0, n1));
					map1.put(key, value);
				} else if (str.contains(" M")) { // M的情况
					n = str.indexOf("/user/") + 6;
					key = str.substring(n);
					n1 = str.indexOf(" M");
					value = Double.parseDouble(str.substring(0, n1));
					map2.put(key, value);
				} else if (str.contains(" K   ")) { // K的情况
					n = str.indexOf("/user/") + 6;
					key = str.substring(n);
					n1 = str.indexOf(" K");
					value = Double.parseDouble(str.substring(0, n1));
					map3.put(key, value);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fin.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

}

3、效果截图

4、附件文件

业务需求 : 根据使用量将下列数据进行排序 然后按照使用量的高低进行 数据展示
分组 --> 排序 --> 抽取数据 007 396.9 K --> java实现
396.9 K   /user/007
593.5 K   /user/12_hl
0         /user/1LB
0         /user/96256
701.3 M   /user/Amanda
611.3 M   /user/ChenQingkang
712.6 M   /user/ChenQingkang12
943.0 K   /user/DAIhe
9.8 M     /user/DAIhe199713
0         /user/Enning
47.9 M    /user/Kung15
5.0 G     /user/LT
105       /user/LZY
611.3 M   /user/LiuChengcheng12
76.4 M    /user/Ruler13
420.6 K   /user/Rulue13
7.0 K     /user/TZM11
673.7 M   /user/WangYun12
0         /user/XXXXX
610.6 M   /user/YC
12.2 M    /user/YuanShiXiong13
31.2 M    /user/anonymous
230       /user/avro
2.8 G     /user/bailina13
105.5 M   /user/baoyu13
1.3 G     /user/begin
618.7 M   /user/caoenning15
673.5 M   /user/caoguangzhou13
730.6 M   /user/caohuyue13
613       /user/caomingyang
4.5 G     /user/caomingyang12
740.4 M   /user/caowenhao13
0         /user/caozhuang
996.1 M   /user/caozhuang12
610.6 M   /user/changjinfang15
0         /user/chaoran2
4.7 G     /user/chenbojie09
9.7 M     /user/chengshaoqi10
1.4 G     /user/chengzhen11
610.6 M   /user/chenjianfei
623.7 M   /user/chenjianfei13
3.6 M     /user/chenjinbiao
1.6 K     /user/chenjinbiao10
611.3 M   /user/chenpeng15
195       /user/chenqingakng12
3.5 G     /user/chenqingkang12
3.5 G     /user/chenshengjian13
412       /user/chentao
611.3 M   /user/chentao15
3.1 G     /user/chenteng09
0         /user/chenxuemeng09
3.6 G     /user/chenyanzhang10
3.1 G     /user/chihongfeng
0         /user/chnegshaoqi10
0         /user/daishirong
735.6 M   /user/daishirong13
4.3 G     /user/dangwenchao12
0         /user/datafile
0         /user/demo
0         /user/dengmingfeng12
0         /user/dengzongxin
4.1 G     /user/dengzongxin13
965.6 M   /user/dmf
1.3 G     /user/donghao
420.7 K   /user/donghao09
170.3 K   /user/donghao10
1.6 G     /user/dongwentao15
2.6 G     /user/doudou
24        /user/doupeng
326.9 M   /user/doupeng08
8.7 K     /user/dr.who
6.2 K     /user/drz
609       /user/dsr
8.7 M     /user/duanheng
3.3 G     /user/duanheng11
2.1 G     /user/duruize08
0         /user/duyang15
3.5 G     /user/duzhuoyan
437.0 K   /user/duzhuoyan10
310       /user/dwt
88.2 M    /user/etlproject11
0         /user/examples
0         /user/fangkun06 
421.0 K   /user/feng11
2.2 G     /user/fengchangxiong
0         /user/fengniu
171.8 K   /user/fengyudong12
665.8 M   /user/fengyudong13
2.7 G     /user/frank
1.7 G     /user/frank12
1.9 G     /user/fuyongquan
0         /user/fuyongquan11
19        /user/gaga.txt
1.8 G     /user/gaochuangxin
426.8 K   /user/gaochuangxin09
226.4 M   /user/gaodan13
333       /user/gaopeng
1.3 G     /user/gaopeng08
3.2 G     /user/gaoyongzhong10
2.4 K     /user/gongtao
0         /user/gongtao08
420.6 K   /user/gongtao09
135.3 M   /user/group_10
615.4 M   /user/guofengshuo13
481.5 M   /user/guorui
1007.8 K  /user/guorui11
0         /user/guoshuaifu15
3.9 G     /user/guoshuzhao15
1.7 G     /user/guoying08
1.2 K     /user/guozhuang10
1.3 G     /user/gushuixa10
3.5 G     /user/gushuxia10
428.6 K   /user/gushuxia1010
409.7 M   /user/h2o2
1.8 G     /user/hadoop
24.9 G    /user/hainiu
31.5 M    /user/hainiu_report
0         /user/hainiu_ypx
0         /user/hainiureport
69        /user/hajipingh12
443       /user/hanzongqing10
2.9 G     /user/haoluyang12
0         /user/haoop
421.0 K   /user/haowenxuan09
54        /user/haozhenchao
3.1 G     /user/haozhenchao09
0         /user/hbase
0         /user/hechaoyong
1.4 M     /user/hehehe
418       /user/hehuanwen
5.6 M     /user/hehuanwen08
1.6 G     /user/hejiping
1020.9 M  /user/hejiping12
507.9 M   /user/hejipint12
13.7 G    /user/hekaifei15
611.3 M   /user/hexiaodong15
1.8 G     /user/heyong07
1.9 M     /user/hezepei
144.4 K   /user/hezhiwei
1.7 G     /user/hezhiwei12
26.6 M    /user/hhs
0         /user/hianiu
2.8 G     /user/hive
0         /user/hive_r
0         /user/hl
610.6 M   /user/hl12
3.3 K     /user/hly
78.2 K    /user/hly12
0         /user/hly12311
1.1 K     /user/hnxywj11
93.9 M    /user/houjun08
0         /user/huahongxin
1.9 G     /user/huahongxin09
614.5 M   /user/huanglei15
2.3 G     /user/huanglibao
1.6 G     /user/huanglingyu11
692.6 M   /user/huangshibo12
388       /user/huangzhaohui
67.5 M    /user/huangzhaohui09
325.3 K   /user/huhongshun123
611.3 M   /user/huhongshun12312
1.1 G     /user/huzewen13
239       /user/input_key
3.2 G     /user/itlishuai
30.1 M    /user/itls
0         /user/jaiyujian07
36.1 M    /user/jf
192       /user/jiadelong
14.0 M    /user/jiadelong10
0         /user/jiakeyao
2.3 M     /user/jiakeyao11
0         /user/jiangliou
429.7 K   /user/jiangliou09
60.5 M    /user/jianglou09
89.6 M    /user/jiangxin10
736.2 M   /user/jiaofeng12
2.6 G     /user/jiayujian
31.8 M    /user/jiayujian07
71.3 K    /user/jiayujianyujian07
1.6 G     /user/jinfu
0         /user/jinfu09
14.0 M    /user/jinfu12
3.2 G     /user/jingxin12
0         /user/jinyanxi
0         /user/jinyanxi10
884       /user/jiping12
57        /user/js_hn
610.6 M   /user/kenan
647.7 M   /user/kongfanxin15
611.4 M   /user/kouxinyuan11
4.9 K     /user/leelongbo11
669.8 M   /user/leizhe13
699.2 M   /user/leizijiao13
0         /user/lenovo
610.6 M   /user/liangsz123113
3.1 G     /user/liangzhiyong09
611.4 M   /user/liaosong15
0         /user/liaoyisi
832.4 M   /user/liaoyisi13
342.5 K   /user/libo
171.8 K   /user/lihong12
1.8 G     /user/lijiarui08
3         /user/lilongbo11
420.7 K   /user/limeiling09
75.6 M    /user/limin08
4.0 K     /user/liming
3.3 G     /user/liming12
2.7 G     /user/linbin15
3.1 G     /user/linjiawei09
1.3 G     /user/linyuhong
614.3 M   /user/linyuhong15
62.6 M    /user/lipengfei10
19        /user/lishengshi
1.3 G     /user/lishuai
4.0 G     /user/lishuai12
939.5 M   /user/lishuai15
206.6 M   /user/liubin13
1.8 G     /user/liubo11
3.0 G     /user/liubo15
1.1 K     /user/liubowen
8.2 M     /user/liubowen08
3.3 G     /user/liuchao12
0         /user/liuhao
2.8 G     /user/liuhong12
610.6 M   /user/liujianyang15
324.5 M   /user/liuming
1016.4 M  /user/liuming12
642.5 M   /user/liumingyang15
49        /user/liuniao
334.5 M   /user/liuniao10
2.1 M     /user/liunioa10
0         /user/liupan
3.4 G     /user/liupan10
468.1 M   /user/liuqaing08
421.3 K   /user/liuqiang08
1023.1 M  /user/liushaopeng11
525.5 K   /user/liushuhe11
0         /user/liuxiaonan15
32.4 M    /user/liuyuhuan
611.3 M   /user/liuyuhuan13
661.2 M   /user/liuzezhuo12
420.6 K   /user/liuzhengxun
31.7 M    /user/liuzhengxun13
420.6 K   /user/liuzhenjiang09
322       /user/liuzhipeng
2.2 M     /user/liuzhipeng09
1.2 G     /user/liuzhu15
290.7 M   /user/lixiaoyao13
36.2 K    /user/liyao
647.1 M   /user/liyuan
672.1 M   /user/liyuan12
468.8 M   /user/lizhifeng10
3.0 G     /user/liziquan12
24        /user/ljiaofeng
0         /user/llblilnogbo
0         /user/llblilnogbo11
8.7 G     /user/llblilongbo11
2.1 G     /user/lp
1.3 G     /user/lpf10
0         /user/lsh
0         /user/lsh11
0         /user/lss12
0         /user/lufeifei10
870       /user/luo
5.1 G     /user/luohantao08
611.3 M   /user/luotao15
425.2 K   /user/luozhenjiu08
25.8 K    /user/luxihao
0         /user/luxihap08
3.5 G     /user/lvsicong10
841.3 K   /user/lxy
4.5 M     /user/lyn
468.3 M   /user/ma11
1.6 G     /user/majixian09
10.3 M    /user/majixian10
586       /user/mapreduce
32        /user/mayaohong10
0         /user/mayucheng
50.4 M    /user/mayucheng10
30        /user/mazhaolaing08
121.2 M   /user/mazhaoliang08
2.2 G     /user/mazhenxin10
0         /user/mengxianbao
68.0 M    /user/mengxianbao08
198.8 M   /user/mitaotao
30.7 M    /user/mitaotao10
1.3 G     /user/mofan10
0         /user/mr
0         /user/mr_input
0         /user/my
5.0 K     /user/nan
1.2 G     /user/nandingding12
6.6 K     /user/nini
48.6 M    /user/nini11
13.8 K    /user/ninifairy
63.2 M    /user/output
21.8 G    /user/panniu
8.7 G     /user/panyifan09
98        /user/pawangpeng12
3.5 M     /user/pd
320.8 M   /user/pd15
0         /user/pd15_out
5.8 G     /user/pengdong15
1.2 G     /user/penghui12
611.3 M   /user/ph
1.5 G     /user/qcy96101111
4.0 K     /user/qiao
5.7 G     /user/qiaoChunYu
0         /user/qiaoqiao
1.9 M     /user/qiaoshengxin09
718.7 M   /user/qiaoshiyu12
21.1 G    /user/qingniu
938.1 M   /user/qiuji13
3.1 G     /user/qiyi
3.1 G     /user/quxin09
115.1 M   /user/renchao
3.6 G     /user/renxiaoting09
0         /user/retail
0         /user/root
0         /user/ruler
3.7 K     /user/ruler13
0         /user/s1
1.1 G     /user/shangmingxin09
4.8 G     /user/shenbo12
416.3 M   /user/shenpeng13
1.8 K     /user/shichaoran
1.5 K     /user/shichaoran.db
31.2 M    /user/shichaoran08
0         /user/shixiaolaing
0         /user/shixiaoliang
11.2 M    /user/shixiaoliang10
0         /user/showtime
108       /user/shu
4.1 G     /user/shuyang
1.6 G     /user/siniu
0         /user/songifen09
0         /user/songjia
3.3 G     /user/songkang12
0         /user/songlifeng009
1.4 G     /user/songlifeng09
0         /user/su
3.4 G     /user/sunchengxun
16.0 K    /user/sunchengxun11
1.2 G     /user/suniu
1.9 G     /user/sunsong08
20.6 M    /user/suntiancheng10
20.2 K    /user/sunxiuhua12
155.2 M   /user/superl
704.9 M   /user/suwenju12
3.2 G     /user/suyuan09
356.0 M   /user/suyuex12
3.8 G     /user/swj
78        /user/t1
0         /user/t8
8.0 M     /user/tengfei11
0         /user/test1
3.0 G     /user/test2
47.0 M    /user/tianguoyan
588.5 K   /user/tianguoyan09
0         /user/tianxia
514.4 M   /user/tianxiaoyu08
3.0 G     /user/tongxilun10
0         /user/user
2.7 G     /user/vince
0         /user/wang
420.7 K   /user/wanganlei11
2.6 G     /user/wangbin12
4.3 G     /user/wangbowen12
707.5 M   /user/wanghan15
328.5 M   /user/wanghao10
1.3 G     /user/wanghaowei10
649.7 M   /user/wanghongyang12
366.4 M   /user/wangjian11
62.7 M    /user/wangjingqin08
583       /user/wangjunteng09
3.8 G     /user/wangjunwei12
20        /user/wangkunyang
1.2 G     /user/wangliumin11
420.6 K   /user/wangliuming11
791.6 M   /user/wanglongchao
3.7 G     /user/wanglongchao12
8.2 G     /user/wangming08
55.3 M    /user/wangmingwei09
884.7 M   /user/wangpeng12
0         /user/wangqiang
3.6 G     /user/wangqiang09
1.8 G     /user/wangqinzheng
2.2 G     /user/wangqinzheng10
1.7 G     /user/wangruilin13
610.6 M   /user/wangruiqiao15
575.2 M   /user/wangshixing
310.5 M   /user/wangshixing08
421.3 K   /user/wangshizi
145.2 M   /user/wangshizi08
2.6 M     /user/wangshuaiang13
450.9 K   /user/wangtongwei10
530       /user/wangwei
90.1 M    /user/wangwei10
0         /user/wangxiaoyu
934       /user/wangyanjie
2.8 G     /user/wangyanjie12
482.2 K   /user/wangyao
195.6 M   /user/wangyao08
2.8 G     /user/wangyu09
1.9 G     /user/wangyue12
629.5 M   /user/wangyun12
810.3 M   /user/wangzhentao15
3.6 G     /user/wangzuowei11
0         /user/wanqh15
5.2 G     /user/wanqiuhao15
0         /user/wanyuan
621.0 M   /user/wanyuan13
64.6 M    /user/wcy
759.0 M   /user/wcy12
644.1 M   /user/weijiaxin12
392.4 M   /user/weishucheng
657.0 M   /user/weishucheng12
239       /user/weiyixuan
700.6 M   /user/weiyixuan12
630.8 M   /user/wenluqi10
14        /user/whw
347       /user/wjq
3.1 G     /user/wjt09
324.9 M   /user/wlq
324.5 M   /user/wtw
673.2 M   /user/wubicheng15
3.1 G     /user/wutenghui09
358.7 K   /user/wuyue
896.2 M   /user/wuyue13
3.6 G     /user/wuzhaohua13
0         /user/wuzhongxiu
423.8 K   /user/wuzhongxiu11
0         /user/wyp
8.2 K     /user/wzt
8.9 K     /user/xiakai10
637.5 M   /user/xiangyongqiao15
0         /user/xiaohaixao09
590.9 K   /user/xiaohaixiao
3.1 G     /user/xiaohaixiao09
174       /user/xiaokey12
4.2 G     /user/xiaokeya12
3.6 G     /user/xiaoying10
1.6 G     /user/xiaoyuan
482       /user/xiaying10
102.6 M   /user/xiedinghong10
0         /user/xieidnghong10
503.3 M   /user/xiexiandong09
1.8 G     /user/xieyunfei11
1.5 G     /user/xjy12
0         /user/xubinjie15
171.2 K   /user/xukun
3.5 G     /user/xukun09
666.9 M   /user/xunianxing13
0         /user/xupengliang
9.0 K     /user/xupengliang09
436       /user/yangbojun09
1023.8 M  /user/yangjianrong15
576.5 M   /user/yangjinyao08
1.3 G     /user/yangjiuyin10
52.9 M    /user/yangkunyi08
4.2 K     /user/yangshu11
0         /user/yangxi
3.0 G     /user/yangxi12
3.1 G     /user/yanjiantong09
170.3 K   /user/yanjiantong9
427.9 K   /user/yanjianwen11
1.3 G     /user/yanjun
249.8 M   /user/yanjun07
0         /user/yankun
1.4 M     /user/yanpeng10
0         /user/yanpengxu
46.4 M    /user/yanpengxu10
298.1 K   /user/yanujun07
968.7 M   /user/yanyao13
702.6 M   /user/yaoyao13
930.2 M   /user/yinjian12
637.0 M   /user/youcong132013
712       /user/ypx
611.3 M   /user/ysc13
46.8 M    /user/ysp
28.5 M    /user/yuanshibo10
0         /user/yuanshixiong13
3.1 G     /user/yuanxiongfei
2.0 G     /user/yuanyingtai
6.1 K     /user/yuanyingtai09
430.6 K   /user/yumaowen
3.5 G     /user/yumaowen10
610.6 M   /user/yuqibo11
3.5 G     /user/yusongping08
298.1 K   /user/yusoongping08
0         /user/zangjiazhen
367.5 M   /user/zangjiazhen10
0         /user/zangjizhen10
1.1 G     /user/zhangbo12
223.2 M   /user/zhangchaoping08
0         /user/zhangchi10
4.4 G     /user/zhangjiaojiao13
3.8 G     /user/zhangjie10
0         /user/zhangkejia09
0         /user/zhangkejia09.db
610.7 M   /user/zhangkenan15
38.0 M    /user/zhanglong
4.4 M     /user/zhanglong06
7.0 G     /user/zhangqi12
3.6 G     /user/zhangxiaoliang11
644.9 M   /user/zhangxiaowei15
2.9 G     /user/zhangxt12
4.9 K     /user/zhangyanni11
589.9 M   /user/zhangyifan
610.6 M   /user/zhangyifan13
1.4 G     /user/zhangyu12
673.7 M   /user/zhaoguang12
41.6 M    /user/zhaolixuan12
58.8 M    /user/zhaomin08
4.6 G     /user/zhaoqiang12
688.6 M   /user/zhaoqiang13
0         /user/zhaotianming10
5.3 G     /user/zhaowenpei09
4.7 K     /user/zhaoxingzhe11
778.7 M   /user/zhaoxingzhe15
621.3 M   /user/zhaoyankun15
9.1 K     /user/zhaoyaokun15
1.0 G     /user/zhengguoguang13
3.4 G     /user/zhengsongsong10
641.9 M   /user/zhijunwei13
9.0 M     /user/zhongjinpeng11
716.7 M   /user/zhoubin123113
0         /user/zhoujian13
3.2 G     /user/zhoujie12
965.4 M   /user/zhouyang
501.2 K   /user/zhouyang12
1.2 G     /user/zhswh13
1.0 K     /user/zhu
621.9 M   /user/zhurundong15
407.8 M   /user/zhwsh13
2.1 G     /user/zjm
22.0 M    /user/zjm12
2.7 G     /user/zmh
1.1 G     /user/zmh12
27.7 M    /user/zouwenyao13
3.0 K     /user/zouxiangyu
76.6 M    /user/zouxiangyu08
757       /user/zouxiangyu09
0         /user/zrd
3.4 G     /user/zzh

 

以上是关于Java读取txt文件,并且对其文件内容进行统计排序的主要内容,如果未能解决你的问题,请参考以下文章

java读取多个txt文件内容,并按照文件名称排序

英文词频统计

用java 读取本地磁盘下的一个txt文件

java 怎么读取文件中的字符和数据

PHP读取文件内容

Java获取本机连接过的所有WiFI密码并且以TXT文本输出文件