统计文件中的URL

1、 题目:

一个文本文件中每一行中有一个URL,最多一万行,统计每一个URL的次数,输出到另外一个文件中,每一行前面是URL,后面是个数。

2、代码:

 1 package test;
 2 
 3 import java.io.*;
 4 import java.util.HashMap;
 5 import java.util.Map;
 6 
 7 public class FileOperate {
 8 
 9     public static void readMethod2() throws IOException {
10         String fileName = "d:/test.txt";
11         String fileName1 = "d:/test1.txt";
12         String line = null;
13 
14         BufferedReader in = new BufferedReader(new FileReader(fileName));
15         BufferedWriter out = new BufferedWriter(new FileWriter(fileName1));
16 
17         Map<String,Integer> map = new HashMap<String,Integer>();
18         int count = 0;
19       //  line = in.readLine();
20         while ((line = in.readLine())!=null) {
21           //  System.out.println(line);
22             if(!map.containsKey(line)){
23                 map.put(line,1);
24             } else{
25                 count = map.get(line);
26                 map.put(line,count+1);
27             }
28             //System.out.println(count);
29             //out.write(line+"
");
30            // line = in.readLine();
31         }
32         for (Map.Entry entry : map.entrySet()
33              ) {
34             entry.getKey();
35             out.write(entry.getKey()+":"+entry.getValue()+"
");
36         }
37 
38 
39         in.close();
40         out.close();
41     }
42 
43     public static void main(String[] args) {
44 
45         try {
46             readMethod2();
47         } catch (IOException e) {
48             e.printStackTrace();
49         }
50     }
51 }
原文地址:https://www.cnblogs.com/yumingxing/p/9469133.html