public static String convertingStr(String lineStr) { String retVal = ""; return retVal; } public static String readPath = "C:\\Users\\zhangqi\\Downloads\\too9.com.txt\\too9.com.txt"; public static String writePath = "D:\\hello.txt"; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { File file = new File(readPath); 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(); if (e1 > e2) { return 1; } else if (e1 < e2) { return -1; } return 0; } }); return es; } public static void printAnyone(Entry<String, Integer>[] es) { IPtest ipTest = new IPtest(); String path = ipTest.getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); if (path.indexOf("WEB-INF") > 0) { path = path.substring(1, path.indexOf("/WEB-INF/")); } path+="/qqwry"; // 指定纯真数据库的文件名,所在文件夹 IPSeeker ip = new IPSeeker("QQWry.Dat", path); // 测试IP 58.20.43.13 System.out.println(path); for (int i = 0; i < es.length; i++) { System.out.println(es[i].getKey() + "出现过 " + es[i].getValue() + "次"); System.out.println(ip.getIPLocation(es[i].getKey().trim()).getCountry()); System.out.println(ip.getIPLocation(es[i].getKey().trim()).getArea()); try { File directory = new File("."); directory.getCanonicalPath();//取得当前路径 String no1 =System.getProperty("user.dir"); 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++){
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class TextConvertor2 {
public static String convertingStr(String lineStr) {
String retVal = "";
return retVal;
} public static String readPath = "C:\\Users\\zhangqi\\Downloads\\too9.com.txt\\too9.com.txt";
public static String writePath = "D:\\hello.txt"; /**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File(readPath); 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(); if (e1 > e2)
{
return 1;
}
else if (e1 < e2)
{
return -1;
}
return 0;
}
}); return es;
} public static void printAnyone(Entry<String, Integer>[] es)
{
IPtest ipTest = new IPtest();
String path = ipTest.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(1, path.indexOf("/WEB-INF/"));
}
path+="/qqwry";
// 指定纯真数据库的文件名,所在文件夹
IPSeeker ip = new IPSeeker("QQWry.Dat", path);
// 测试IP 58.20.43.13
System.out.println(path);
for (int i = 0; i < es.length; i++)
{
System.out.println(es[i].getKey() + "出现过 " + es[i].getValue() + "次"); System.out.println(ip.getIPLocation(es[i].getKey().trim()).getCountry());
System.out.println(ip.getIPLocation(es[i].getKey().trim()).getArea()); try {
File directory = new File(".");
directory.getCanonicalPath();//取得当前路径
String no1 =System.getProperty("user.dir"); 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++){
out.write(ip.getIPLocation(es[i].getKey().trim()).getCountry().getBytes()); //向文件中写入数据
out.write(ip.getIPLocation(es[i].getKey().trim()).getArea().getBytes()); //向文件中写入数据
out.write(es[i].getValue().toString().getBytes());
out.write('\r'); // \r\n表示换行
out.write('\n');
out.close(); //关闭输出流
System.out.println("写入成功!");
}catch(IOException e){
e.printStackTrace();
}
}}
public static void printMax(Entry<String, Integer>[] es)
{
Entry<String, Integer> max = es[es.length - 1];
IPtest ipTest = new IPtest();
String path = ipTest.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if (path.indexOf("WEB-INF") > 0) {
path = path.substring(1, path.indexOf("/WEB-INF/"));
}
path+="/qqwry";
// 指定纯真数据库的文件名,所在文件夹
IPSeeker ip = new IPSeeker("QQWry.Dat", path);
// 测试IP 58.20.43.13
System.out.println(path);
System.out.println(max.getKey() + "出现过" + (Integer) max.getValue()+"次");
System.out.println(ip.getIPLocation(max.getKey().trim()).getCountry());
System.out.println(ip.getIPLocation(max.getKey().trim()).getArea());for (int i = es.length - 2; i > -1; i--)
{
if (es[i].getValue().intValue() == max.getValue().intValue())
{
System.out.println((String) es[i].getKey() + "出现过" + (Integer) es[i].getValue() + "次"); }
else
{
break;
}
}
}
}