统计一个文件中重复行的个数,并打印出内容

分析试题

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));
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值