统计一个文件中重复行的个数,并打印出内容
2015-01-30 14:11
169 查看
分析试题
1、统计文件,肯定涉及到文件的读取操作,考察IO的操作。
2、统计这块,考察的是集合框架的Map集合的添加,遍历
1、统计文件,肯定涉及到文件的读取操作,考察IO的操作。
2、统计这块,考察的是集合框架的Map集合的添加,遍历
public static void test(String filepath) { try { File file = new File(filepath); if(!file.exists()) { System.out.println("file not exist"); return; } //create BufferedReader to improve efficient BufferedReader bufReader = new BufferedReader(new FileReader(file)); String line = null; //create map collection to record information Map<String,Integer> map = new HashMap<String,Integer>(); while((line = bufReader.readLine()) != null) { if(map.containsKey(line)) map.put(line,map.get(line)+1); else map.put(line,1); } //print map collction showMap(map); } catch (Exception ex) { ex.printStackTrace(); } } private static void showMap(Map<String,Integer> map) { if(map == null) return; Set<String> keyset = map.keySet(); Iterator<String> it = keyset.iterator(); while(it.hasNext()) { String s = it.next(); System.out.println( s+ "......" + map.get(s)); } }
相关文章推荐
- C语言打开一个文件(有内容),倒序打印出文件中的内容
- 将一个文本文件内容反序打印出来(包含中文时)
- 定义一个文件输入流,调用read(byte[] b)方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5)。
- 定义一个文件输入流,调用read(byte[] b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)
- java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号
- 【每天一个Linux命令】26. 文件内容统计命令wc
- 统计文件中单词出现的行号及打印出该行内容
- 7、定义一个文件输入流,调用read(byte[] b) 方法将exercise.txt文件中的所有内容打印出来(byte数组的大小限制为5,不考虑中文编码问题)。
- 统计一个区域中不重复的单元格内容数量
- 在一个文本文件中的单词统计频率并打印前十个
- 采用多线程和生产者消费者模式来实现对于一个目录以及所有子目录下的文件内容的搜索,打印出包含指定关键字的行.
- day20打印流,删除一个带内容的目录,用于记录应用程序运行次数,多个读取流变成一个读取流,切割文件
- 实现一个命令行linux终端应用程序,把输入的指定文件,映射到内存中,并打印出文件内容
- IO操作文件内容打印到控制台,将文件内容复制到另外一个文件
- 定义一个文件输入流,调用 read(byte[] b)方法将 exercise.txt 文件中的所有内容打印出来 (byte 数组的大小限制为 5)。
- 编写一个程序,从标准输入一行一行地读取文本,并完成如下任务:如果文件中有两行或更多行相邻的文本内容相同,那么就打印其中一行,其余行不打印。
- loner_li 机试题 一个文本文件含有如下内容,分别表示姓名和成绩:张三 90 ..提供用户一个控制台界面,允许用户输入要查询的姓名,输入姓名并且按回车以后,打印出此人的成绩...
- C语言实现打开一个文件(有内容的),统计这个文件中字符e出现了多少次
- 打开一个txt文件,读取里面的内容并打印出来
- 给定一个英文原文,统计文件里面一共有多少个不同的英文单词