Chinaunix首页 | 论坛 | 博客

acc

  • 博客访问: 802679
  • 博文数量: 170
  • 博客积分: 7011
  • 博客等级: 少将
  • 技术积分: 1660
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-31 12:02
文章分类

全部博文(170)

文章存档

2014年(7)

2010年(2)

2009年(62)

2008年(25)

2007年(67)

2006年(7)

我的朋友

分类: Java

2009-09-17 18:17:42

package test;


public class Astro {
    public static void main(String args[]) {
        get();
    }
    public static void get(){
//        String url [][]={

//                {"Sina_Today",""},

//                {"Sina_Tomorrow",""},

//                {"TQ",""},

//                

//        };

//        for(int i=0;i
//                saveFile("C:\\TEMP\\"+url[i][0]+".html",getHtml(url[i][1]));


        System.out.println(getTemp());
    }
    public static String getImgPath(){
        String html=getHtml("");
        String regex=";
        String path="images/sun.png";
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher m = pattern.matcher(html);
        if(m.find())path=m.group(1);
        return path;
    }
    public static String getTemp(){
        String html=getHtml("");
        String t="20";
        String regex="([0-9]+)℃~([0-9]+)℃";
        System.out.println(regex);
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
        java.util.regex.Matcher m = pattern.matcher(html);
        int g=0;int d=0;
        if(m.find()){
            g=Integer.parseInt(m.group(2));
            d=Integer.parseInt(m.group(1));
        }
        t=String.valueOf((d+g)/2);
        return t;
    }
    public static String getHtml(String urlString) {
        try {
            StringBuffer html = new StringBuffer();
            java.net.URL url = new java.net.URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            java.io.InputStreamReader isr = new java.io.InputStreamReader(conn.getInputStream());
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            String temp;
            while ((temp = br.readLine()) != null) {
                html.append(temp).append("\n");
            }
            br.close();isr.close();    return html.toString();
        } catch (Exception e) {    e.printStackTrace();return null;}
    }

//    public static void saveFile(String ff, String s) {

//        try {

//            File f = new File(ff);

//            PrintWriter flow = new PrintWriter(new FileWriter(f));

//            flow.println(s);

//            flow.close();

//        } catch (IOException e) {

//            e.printStackTrace();

//        }

//    }

}

阅读(1322) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~