Java统计一个文本文件中每一行字符串出现的次数

需求:现要统计一个文本文件中每一行字符串出现次数

Java代码实现如下(点击此处下载代码工具类):

public static HashMap analysizeFile(File file) {
	HashMap res = new HashMap();
	BufferedReader br = null;
	try {
		br = new BufferedReader(new FileReader(file));
		String readLine = null;
		while ((readLine = br.readLine()) != null) {
			Integer ret = new Integer(0);
			if (res.containsKey(readLine))
				ret = res.get(readLine);
			res.put(readLine, ret + 1);
		}
		return res;
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}


你可能感兴趣的:(Java)