高手进!!帮忙解释一下!
第一段:package util;import java.util.ArrayList;
import java.util.Collections;
import java.
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class DataHandle
{
public boolean exportXml(String filePath , String outPath)
{
List outList = getVisitAnnal(filePath);
List xmlList = getXMLList(outList);
return WriteUtil.write(outPath, xmlList);
}
private List getXMLList(List outList)
{
List result = new ArrayList();
result.add("<root>");
for (int i = 0; i < outList.size(); i++)
{
List tempList = (List)outList.get(i);
result.add(" <node>");
result.add(" <uri>" + tempList.get(1) + "</uri>");
result.add(" <count>" + tempList.get(0) + "</count>");
result.add(" </node>");
}
result.add("</root>");
return result;
}
/*
* 通过日志路径获得每个IP请求的次数
*/
public List getVisitAnnal(String filePath)
{
List readList = ReadUtil.read(filePath);
Set allSet = new HashSet();
List filterList = filterData(readList, allSet);
List result = countTimes(filterList, allSet);
return result;
}
/*
* 统计每个IP出现的次数,并排序
*/
private List countTimes(List list, Set set)
{
List disOrderList = new ArrayList();
Iterator io = set.iterator();
while (io.hasNext())
{
List tempMap = getMap(io.next().toString(), list);
disOrderList.add(tempMap);
}
orderList(disOrderList);
return disOrderList;
}
/*
* 排序
*/
private void orderList(List list)
{
Collections.sort(list, new Comparator()
{
public int compare(Object obj, Object obj2)
{
List lsit1 = (List)obj;
Integer num1 = (Integer)lsit1.get(0);
List lsit2 = (List)obj2;
Integer num2 = (Integer)lsit2.get(0);
if(num1.intValue() > num2.intValue())
{
return -1;
}
else
{
return 1;
}
}
});
}
/*
* 得到一个IP出现次数的Map
*/
private List getMap(String ip, List list)
{
List result = new ArrayList();
int count = 0;
for (int i = 0; i < list.size(); i++)
{
String str = list.get(i).toString();
if (ip.equals(str))
{
count++;
}
}
result.add(new Integer(count));
result.add(ip);
return result;
}
/*
* 得到所有IP的集合
*/
private List filterData(List list, Set set)
{
List result = new ArrayList();
for (int i = 0; i < list.size(); i++)
{
String[] temp = list.get(i).toString().split(" ");
String tempStr = temp[7];
set.add(tempStr);
result.add(tempStr);
}
return result;
}
public static void main(String[] args)
{
DataHandle obj = new DataHandle();
List resultList =obj.getVisitAnnal("k:/dataset.log");
/*
JSONArray result = new JSONArray();
for (int i = 0; i < resultList.size(); i++)
{
List list = (List)resultList.get(i);
System.out.println(list);
JSONObject temp = new JSONObject();
//temp.putOpt(key, value)
try
{
temp.putOpt("url" ,list.get(1) );
temp.put("count" , list.get(0));
}
catch (JSONException e)
{
e.printStackTrace();
}
result.put(temp);
}
System.out.println(result.toString());
*/
boolean isSuccess = obj.exportXml("k:/dataset.log" , "k:/out.xml");
/*
int resultCode = 0;
if(isSuccess)
{
resultCode = 1;
}
JSONObject result = new JSONObject();
try
{
result.put("resultCode" , resultCode);
}
catch (JSONException e)
{
e.printStackTrace();
}
System.out.println(result);
*/
}
}
第二段:
package util;
import
import
import
import
import
import java.util.ArrayList;
import java.util.List;
public class ReadUtil
{
public static List read(String fileName)
{
List list = new ArrayList();
BufferedReader br = null;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null)
{
if(line.indexOf(".asp") != -1)
{
list.add(line);
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return list;
}
}
第三段:
package util;
import
import
import
import
import java.util.List;
public class WriteUtil
{
public static boolean write(String fileName , List list)
{
PrintWriter pw = null;
try
{
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName , false)));
for (int i = 0; i < list.size(); i++)
{
pw.println((String)list.get(i));
}
pw.flush();
return true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return false;
}
finally
{
if (pw != null)
{
pw.close();
}
}
}
}