| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 420 人关注过本帖
标题:高手进!!帮忙解释一下!
只看楼主 加入收藏
andongtianzi
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-6-29
结帖率:40%
收藏
已结贴  问题点数:5 回复次数:1 
高手进!!帮忙解释一下!
第一段: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();
            }
        }
    }
}
搜索更多相关主题的帖子: 解释 
2010-07-04 22:27
erfen
Rank: 2
等 级:论坛游民
帖 子:9
专家分:15
注 册:2010-5-30
收藏
得分:5 
- -  JAVA   发错地方了吧
2010-07-04 22:29
快速回复:高手进!!帮忙解释一下!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032570 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved