1,a,28 2,b,35 3,c,28 4,d,35 5,e,28 6,a,28 7,b,35 8,c,28 9,a,28
案例
public class FileTest { static File filea = new File("C:\\Temp\\1\\a.txt"); static HashMap<String, Integer> hashmap = new HashMap<String, Integer>(); public static void main(String[] args) throws IOException { BufferedReader bra = new BufferedReader(new FileReader(filea)); Scanner sa = new Scanner(bra); while (sa.hasNextLine()) { String line = sa.nextLine(); String name = line.split(",")[1]; if (hashmap.containsKey(name)) hashmap.put(name,hashmap.get(name) + 1) else hashmap.put(name, 1); } for (Entry<String, Integer> entry : hashmap.entrySet()) { Object key = entry.getKey(); Object val = entry.getValue(); System.out.println(key.toString() + " " + val.toString()); } } }
d 1 e 1 b 2 c 2 a 3