分析试题
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));
}
}