int index=0;
List <String> ip3 = new ArrayList<String>();
//String[] arr = new String[ip3.size()];
//String[] ip3=new String[400000];//result
if (!file.exists()) {
System.out.println("File not exist!");
System.exit(0);
}
BufferedReader br = new BufferedReader(new FileReader(readPath));
// BufferedWriter bw = new BufferedWriter(new FileWriter(writePath));
String temp = "";
index =0;
String[] arr = ip3.toArray(new String[1111111]);
while ((temp = br.readLine()) != null) {
Pattern p = Pattern.compile(":\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s*:");//这个匹配还是有点取巧了。
//可以这样":\\s*(\\d+\\.\\d+\\.\\d+\\.\\d+)\\s*:"安全一点
Matcher m = null;
if(!"".equals(temp)){
m = p.matcher(temp);
while(m.find()){
arr[index] = m.group(1);
index++;
break;
}
}
// System.out.println(door);
//把变量存入数组
//ip3.add(door);
// ip3[index++] = door;
// index++;
}// end of while
for (int i=0;i<index;i++){
//System.out.println(i);
// System.out.println(ip3[i]);
}
// end of for
// end of while
//String[] data = Arrays.copyOfRange(ip3, 0,index);//用另外一个字符串替换。
Map.Entry<String, Integer>[] es = handle(arr);
System.out.println("(每个余元素出现过的次数)");
printAnyone(es);
System.out.println("(出现过次数最多的元素)");
printMax(es);
br.close();
}// end of main
public static Entry<String, Integer>[] handle(String[] arr)
{
Map<String, Integer> m = new HashMap<String, Integer>();
for (int i = 0; i < arr.length; i++)
{
String k = new String(arr[i]);
if (!m.containsKey(k))
{
m.put(k, new Integer(1));
}
else
{
Integer v = (Integer) m.get(k);
v = new Integer(v.intValue() + 1);
m.put(k, v);
}
}
Set<Entry<String, Integer>> s = m.entrySet();
Entry<String, Integer>[] es = s.toArray(new Map.Entry[] { null });
java.util.Arrays.sort(es, new Comparator()
{
public int compare(Object o1, Object o2)
{
int e1 = ((Map.Entry<String, Integer>) o1).getValue()
.intValue();
int e2 = ((Map.Entry<String, Integer>) o2).getValue()
.intValue();
File f = new File(directory+directory.separator+"ip.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out;
out = new FileOutputStream(f,true);
// int j=0;
//for(j=0;j<arr.length;j++){